php - if block inside echo statement? -
i suspect it's not allowable because getting "parse error: syntax error, unexpected t_if in..." error. couldn't find way accomplish goal. here's code:
<?php $countries = $myaddress->get_countries(); foreach($countries $value){ echo '<option value="'.$value.'"'.if($value=='united states') echo 'selected="selected"';.'>'.$value.'</option>'; } ?>
what displays list of countries in select element , sets united states default. doesn't work sadly...
you want use the ternary operator acts shortened if/else statement:
echo '<option value="'.$value.'" '.(($value=='united states')?'selected="selected"':"").'>'.$value.'</option>';