71

I suspect it's not allowable because I am getting "Parse error: syntax error, unexpected T_IF in..." error. But I couldn't find a way to accomplish my goal. Here's my code:

<?php $countries = $myaddress->get_countries(); foreach($countries as $value){ echo '<option value="'.$value.'"'.if($value=='United States') echo 'selected="selected"';.'>'.$value.'</option>'; } ?> 

What it does is it displays a list of countries in a select element and sets United States as the default. I doesn't work sadly...

4 Answers 4

187

You will want to use the a ternary operator which acts as a shortened IF/Else statement:

echo '<option value="'.$value.'" '.(($value=='United States')?'selected="selected"':"").'>'.$value.'</option>'; 
Sign up to request clarification or add additional context in comments.

5 Comments

So that's where ternary operator is useful. That was quick, thanks a lot!
Its not "my preference" in situations like this. I like the ternary operator for defaulting / setting variables. But given that is what you asked, there is how you do it :)
It’s a ternary operator but not the ternary operator (although I don’t know any other ternary operator). Another common term is conditional operator.
Yea an easily overlooked mistake on my part. Fixed it Gumbo.
'.(($value=='United States')?'selected="selected"':"").' can also be value="'.(($value=='')?' selected ':"").' the first is supposedly XHTML but just using 'selected' should work just fine. Is a bit simpler.
20

You can always use the ( <condition> ? <value if true> : <value if false> ) syntax (it's called the ternary operator - thanks to Mark for remining me :) ).

If <condition> is true, the statement would be evaluated as <value if true>. If not, it would be evaluated as <value if false>

For instance:

$fourteen = 14; $twelve = 12; echo "Fourteen is ".($fourteen > $twelve ? "more than" : "not more than")." twelve"; 

This is the same as:

$fourteen = 14; $twelve = 12; if($fourteen > 12) { echo "Fourteen is more than twelve"; }else{ echo "Fourteen is not more than twelve"; } 

1 Comment

As @Gumbo pointed out, this is just one ternary operator, not "the". "Another common term is conditional operator."
11

Use a ternary operator:

echo '<option value="'.$value.'"'.($value=='United States' ? 'selected="selected"' : '').'>'.$value.'</option>'; 

And while you're at it, you could use printf to make your code more readable/manageable:

printf('<option value="%s" %s>%s</option>', $value, $value == 'United States' ? 'selected="selected"' : '' $value); 

Comments

8

In sake of readability it should be something like

<?php $countries = $myaddress->get_countries(); foreach($countries as $value) { $selected =''; if($value=='United States') $selected ='selected="selected"'; echo '<option value="'.$value.'"'.$selected.'>'.$value.'</option>'; } ?> 

desire to stuff EVERYTHING in a single line is a decease, man. Write distinctly.

But there is another way, a better one. There is no need to use echo at all. Learn to use templates. Prepare your data first, and display it only then ready.

Business logic part:

$countries = $myaddress->get_countries(); $selected_country = 1; 

Template part:

<? foreach($countries as $row): ?> <option value="<?=$row['id']?>"<? if ($row['id']==$current_country):> "selected"><? endif ?> <?=$row['name']?> </option> <? endforeach ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.