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>'; 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo