Using the WordPress selected() function

The selected function causes immediate output, it does not return a string. Therefore you can’t use it like that, in a string building mode.

If you want it to return a string instead of immediately outputting the text, pass false to the $echo parameter.

$string = "
<select name="gender">
<option value="male" " . selected( $values['gender'] , 'male', false) . ">Male</option>
<option value="female" " . selected( $values['gender'] , 'female', false) . ">Female</option>
<option value="other" " . selected( $values['gender'] , 'other', false) . ">Other</option>         
</select>
";

error code: 523