Replace two instances of
echo "foo='YES'" >> /etc/rc.conf
with
sysrc foo=yes
which is shorter and provides additional error checking.