在 PHP 中,可以进行条件变量替换吗?
我想知道 PHP 中是否可以执行以下操作:
<?php echo "Hello {isset($name) ? $name : 'World'}!\n"; ?>
或者我必须执行以下操作吗?
<?php echo "Hello " . ( isset($name) ? $name : 'World' ) . "!\n"; ?>
一般来说,我更喜欢替换而不是串联,因为我发现字符串串联会使字符串过长而难以阅读。
其上下文是我正在根据数据库查询来决定默认情况下应选择选择标记中的哪个选项。
完整的上下文:
<?php
echo "
<form action='add_demographics.php' method='post'>
<table>
<input type=hidden name=userId value='{$_SESSION['username']}'/>
<tr><td>User ID:</td><td>{$_SESSION['username']}</td></tr>";
foreach ($inputLabels as $i => $value)
{
echo "
<tr>
<td>{$value['text']}</td>
<td><select name=$i>";
foreach ($value["options"] as $optionName => $optionValue)
{
echo "
<option value=$optionValue {($result[$i]==$optionValue ? ' selected=true' : '')}>$optionValue</option>";
}
echo "
</select></td>
</tr>";
}
echo "
<tr><td><input type='submit' name='submit' value='Submit' /></td></tr>
</table>
</form>";
?>
I was wondering if something like the following would be possible in PHP:
<?php echo "Hello {isset($name) ? $name : 'World'}!\n"; ?>
or do I have to do the following?
<?php echo "Hello " . ( isset($name) ? $name : 'World' ) . "!\n"; ?>
In general I prefer substitution over concatenation as I find that string concatenation can make strings harder to read by making them overly long.
The context of which is that I'm deciding which option in a select tag should be selected by default based on a database query.
The full context:
<?php
echo "
<form action='add_demographics.php' method='post'>
<table>
<input type=hidden name=userId value='{$_SESSION['username']}'/>
<tr><td>User ID:</td><td>{$_SESSION['username']}</td></tr>";
foreach ($inputLabels as $i => $value)
{
echo "
<tr>
<td>{$value['text']}</td>
<td><select name=$i>";
foreach ($value["options"] as $optionName => $optionValue)
{
echo "
<option value=$optionValue {($result[$i]==$optionValue ? ' selected=true' : '')}>$optionValue</option>";
}
echo "
</select></td>
</tr>";
}
echo "
<tr><td><input type='submit' name='submit' value='Submit' /></td></tr>
</table>
</form>";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,不能那样做。您需要在字符串之外执行此操作并连接。 php 在字符串中唯一要做的就是查找并计算单个 $var (无表达式) - 如果您使用双引号或等效的heredoc(php 不会计算单引号中的变量)
nope, cannot do that. You need to do it outside of the string and concat. The only thing php will do inside a string is look for and evaluate a single $var (no expressions) - if you use double quotes or equivalent heredoc (php does not evaluate vars in single quotes)
不,不可能按照你要求的方式去做。
然而,解决您的特定问题的方法可能是在 foreach 循环的顶部分配一个变量来保存是否应选择该选项,然后回显该变量作为输出的一部分:
No, it isn't possible to do it the way you're asking.
A solution to your specific problem, however, could be to assign a variable at the top of the foreach loop to hold whether or not that option should be selected, then echo that variable as part of your output:
也许是因为人手不足呢?
Maybe for short-handing it?
你可以做这样的事情:
来自 webbiedave 的更新:
“永远不要这样做,因为使用三元运算符来引用丢弃变量只是为了避免串联,这绝对是一种残暴的编码实践!
You could do something like this:
UPDATE from webbiedave:
"Don't ever do this as it's absolutely atrocious coding practice to use the ternary operator to reference a throw away variable variable simply to avoid concatenation!
鉴于你想做的事情。我想说
Given what you want to do. I'd say
实际上从来没有需要它。
学习使用模板。
HTML应该是HTML,而不是PHP中硬编码的一些抽象数据
It is never actually required.
Learn to use templates.
HTML should be HTML, not some abstract data hardcoded in PHP