php 如果在 echo 语句中阻塞?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3507042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if block inside echo statement?
提问by Joann
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:
我怀疑这是不允许的,因为我收到“解析错误:语法错误,意外的 T_IF in...”错误。但我找不到实现目标的方法。这是我的代码:
<?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...
它的作用是在选择元素中显示国家/地区列表,并将美国设置为默认值。我没有工作可悲...
回答by Jim W.
回答by Frxstrem
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 :)).
你总是可以使用( <condition> ? <value if true> : <value if false> )
语法(它被称为三元运算符-感谢 Mark 提醒我:))。
If <condition>
is true, the statement would be evaluated as <value if true>
. If not, it would be evaluated as <value if false>
如果<condition>
为真,则该语句将被评估为<value if true>
。如果不是,它将被评估为<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";
}
回答by Ed Mazur
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
来使您的代码更具可读性/可管理性:
printf('<option value="%s" %s>%s</option>',
$value,
$value == 'United States' ? 'selected="selected"' : ''
$value);
回答by Your Common Sense
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.
但还有另一种方式,一种更好的方式。根本不需要使用 echo。学习使用模板。先准备好数据,然后才显示出来。
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 ?>