Not exactly
The variable $sex I am assuming you are storing in the database, etc. When the user returns to the page to decide upon what gender he/she/it is the variable should be set, eg $sex = "Unsure".
You have set the array correctly, but the foreach statement is incorrect and the array name would clash with the users origional choice, if you were to set the array as $sexOptions and the origional value is $sex that wont be a problem.
where you have wrote: foreach($row as $sex => $val) should read: foreach($sexOptions as $value)
With that you are sourcing each value of the array $sexOptions. The key of the array is irrelevent so you don't need the =>
the if($sex == $row
) should propbably read: if($value == $sex) where it is checking against the $value of the array $sexOptions to see if it is the same. The rest is fine.
$sex = "Unsure"; // setting this so you can see the result, it really should be taken from a database if the form has been filled out by the user previously.
$sexOptions = array ('Male', 'Female', 'Unsure');
echo "<select name=\"sex\">\n";
foreach($sexOptions as $value) {
echo "<option";
if($value == $sex) {
echo "selected";
}
echo ">$value</option>\n";
}
echo "</select>\n";
You will see that the select box has "Unsure" already selected.
Hope this helps