PHP 三元内部串联没有其他?

标签 php string concatenation ternary-operator ternary

<分区>

我想检查 2 个变量是否相同,如果相同,则回显一个字符串。这在串联中可能吗?并在不创建单独函数的情况下做到这一点?

例如

$var = '这里是第一部分和'。 ($foo == $bar)? “可选的中间部分”。和字符串的其余部分。'

编辑

请注意,我正在寻找没有 : '' 的方法。如果您愿意,可以使用“二元运算符”。

最佳答案

不要试图将事情缩短太多。您需要 : '' 才能正常工作。

使用(条件)? "show when true": "" 根据条件显示可选文本。三元运算符之所以这样命名,是因为它由 3 个部分组成。

$var = 'here is the first part and '. (( $foo == $bar ) ? "the optional middle part" : "") .' and the rest of the string.';

关于PHP 三元内部串联没有其他?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15163467/

相关文章:

javascript - 如何使用 PHP 将数据保存在我的数据库中

php - IIS 网站 "failed to open stream: No such file or directory in C:\inetpub\vhosts\"但那里存在文件

php - 使用php转换为base64

ruby-on-rails - 如何分隔 ruby 中的括号?

c - strtok 如何以及为什么记住字符串?

c - C 中的字符串连接函数不执行任何操作,那么正确的方法是什么?

Mysql串联

php - 无法在 php 的 stdClass 属性中分配闭包

vb.net - 将UTF-8网页下载到String中

javascript - 在 Node.js 中连接或合并文件的最佳方法