echo ('True'?(true?'t':'f'):'False'); 的输出可能是什么并解释为什么?
我接受采访时遇到了一个非常基本的 PHP 问题,就像:
echo ('True' ? (true ? 't' : 'f') : 'False');
有人可以解释它将产生的输出的详细信息吗?
谢谢
Possible Duplicate:
What is the PHP ? : operator called and what does it do?
i was interviewed with a very basic question of PHP which was like:
echo ('True' ? (true ? 't' : 'f') : 'False');
Can Someone explain the details of the output it will yield?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这将回显t。
因为第一个它会检查第一个条件是否为真。
之后,在下一个条件中,它再次给出 true 并执行第一个条件,即 t。
在if和else条件下会写成如下:
This will echo t.
Because of first it will check the first condition that will give true.
and after that in next condition it again give true and execute the first condition that is t.
In if and else condition it will be write as follow:
看看这个版本应该就清楚了:
Looking at this version should make it clear:
除字符串
"0"
之外,非空字符串被视为真值。 计算PHP从左到右
'True'
被计算为布尔 true?
后面的表达式true
是... true!?
后面的表达式,即t令人惊讶的是,如果表达式为:
A non-empty string is considered a truthy value except the string
"0"
. PHP evaluatesfrom left to right as follows:
'True'
is evaluated as a boolean true?
is evaluatedtrue
is... true!?
is returned, which is tSurprisingly, you'll still get t if the expression was:
由于
'True'
被评估为true
As
'True'
is Evaluated astrue
内部括号将首先执行
true?'t':'f'
它将返回 't',它是一个字符串现在外部条件将检查
echo ('True' ? 't ':'假')
。这里'True'
是一个“非空字符串”(隐式转换为 TRUE),因此它的计算结果为 true,并将返回 't'。最终代码将是
echo ('t')
,它将简单地 echo tThe inner bracket will be executed first
true?'t':'f'
it will return 't' that is a stringNow outer condition will check for
echo ('True' ? 't' : 'False')
. Here'True'
is a "non empty string"(implicitly casted to TRUE), so it evaluate to true, and will return 't'.Final code will be
echo ('t')
which will simply echo t这:
可以写成
This:
May be written as