[英]Correct PHP Syntax with IF and Echo
I was hoping I could get some help from you guys. 我希望我能从你们那里得到一些帮助。 The code below works but I doubt that this is a good way to write the syntax. 下面的代码有效,但是我怀疑这是编写语法的好方法。 Especially since it will require an enormous amount of code. 特别是因为这将需要大量的代码。
The thing I'm trying to do is create a list which will be populated only if an item has a value. 我正在尝试做的事情是创建一个列表,仅当一个项目具有值时才填充该列表。 I have already specified if the variable has a value or not (I think that part of the code could use some work as well, but that's a different question). 我已经指定了变量是否具有值(我认为部分代码也可以使用某些功能,但这是一个不同的问题)。 And I've found a way to show or hide the item in the list. 而且我找到了一种显示或隐藏列表中项目的方法。 But I don't think it's efficient. 但是我不认为这是有效的。 And that's where I could use some help. 那是我可以使用一些帮助的地方。
<?php
if(empty($orderNum))
{
echo "<li><a href='#' title='Brodit'>Tomte</a></li>";
}
else
{
echo "<li><a href='unordered.php?SupName=2&SupStatus=3' title='Brodit'>Order ("; echo "$orderNum"; echo ")</a></li>";
}
?>
You can use php alternative syntax widely used in php templates and suggested in docs like zend manual as well as symfony: 您可以使用php模板中广泛使用的php替代语法,并在诸如zend manual和symfony之类的文档中建议使用:
<?php if(empty($orderNum)): ?>
<li><a href="#" title="Brodit">Tomte</a></li>
<?php else: ?>
<li><a href="unordered.php?SupName=2&SupStatus=3"title="Brodit">
Order (<?php echo $orderNum; ?>)</a>
</li>
<?php endif; ?>
<?php
if(empty($orderNum)){
echo "<li><a href='#' title='Brodit'>Tomte</a></li>";
} else {
echo "<li><a href='unordered.php?SupName=2&SupStatus=3' title='Brodit'>Order ({$orderNum})</a> </li>";
}
?>
You can use "foo {$var} bar"
instead of "foo ".$var." bar"
. 您可以使用"foo {$var} bar"
代替"foo ".$var." bar"
。 This is (in my opinion) more elegant. (在我看来)这更优雅。
If the HTML Code gets longer I would prefer: 如果HTML代码变得更长,我希望:
<?php if(empty($orderNum)): ?>
<li><a href="#" title="Brodit">Tomte</a></li>
<?php else: ?>
<li><a href="unordered.php?SupName=2&SupStatus=3"title="Brodit">
Order (<?php echo $orderNum; ?>)</a>
</li>
<?php endif; ?>
To add another alternative to what was already answered, you could focus on the HTML rather than the PHP: 要为已回答的问题添加另一种选择,您可以专注于HTML而不是PHP:
<?php if(empty($orderNum)) { ?>
<li><a href='#' title='Brodit'>Tomte</a></li>
<?php } else {?>
<li><a href='unordered.php?SupName=2&SupStatus=3' title='Brodit'>Order ("<?php echo $orderNum;?>")</a></li>";
<?php }?>
This especially useful if you want to take advantage of your HTML editor (syntax validation, auto-completion, ...). 如果您想利用HTML编辑器(语法验证,自动完成等),这特别有用。 I would choose this syntax for big chunks of HTML and when working with integrators, because it is less likely that they will screw up the PHP part. 在与集成商合作时,我会为大量的HTML选择此语法,因为他们不太可能搞砸PHP部分。
<?php
if(empty($orderNum))
{
echo "<li><a href='#' title='Brodit'>Tomte</a></li>";
}
else
{
echo "<li><a href='unordered.php?SupName=2&SupStatus=3' title='Brodit'>Order (". $orderNum .")</a></li>";
}
?>
One thing you could do is assign the string to a variable and then have one echo statement at the end. 您可以做的一件事是将字符串分配给变量,然后在末尾添加一个echo语句。 Also you can do variable substitution in the double quotes. 您也可以在双引号中进行变量替换。 For example: 例如:
"Order ($orderNum)" “订单($ orderNum)”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.