将 echo 和 print 组合在一个语句中 [英] Combine an echo and a print in one statement
问题描述
echo "1" .(print '2') + 3;
返回 214. 脚本如何以 *14 结尾?
echo "1" . (print '2') + 3;
returns 214. How does the script end up with *14?
推荐答案
什么时候做
echo "1" . (print '2') + 3;
PHP 会做(演示)
line # * op fetch ext return operands
---------------------------------------------------------------------------------
2 0 > PRINT ~0 '2'
1 CONCAT ~1 '1', ~0
2 ADD ~2 ~1, 3
3 ECHO ~2
4 > RETURN 1
一句话:
- 打印2,返回1
- 连接1"并返回 1 =>11"
- 添加11"+ 3 => 14
- 回声 14
那是 214.
运算符 + - .
具有相同的 运算符优先级,但保持关联:
The operators + - .
have equal Operator Precedence, but are left associative:
对于优先级相等的运算符,左结合性表示求值从左到右进行,而右结合性则相反.
For operators of equal precedence, left associativity means that evaluation proceeds from left to right, and right associativity means the opposite.
<小时>
因为所有其他答案都声称 PHP 是 1+3,这里进一步证明它不是:
since all the other answers claim PHP does 1+3, here is further proof that it doesnt:
echo "1" . (print '2') + 9;
给出 220,例如11+9 而不是 1 .(1+9)
.如果加法优先于串联,则为 2110,但为此您必须编写
gives 220, e.g. 11+9 and not 1 . (1+9)
. If the addition had precedence over the concatenation, it would have been 2110, but for that you'd had to write
echo "1" . ((print '2') + 9);
这篇关于将 echo 和 print 组合在一个语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!