問題描述
下面的代碼輸出43211
,為什么?
The following code outputs 43211
, why?
echo print('3').'2'.print('4');
推薦答案
您的語句將解析為人類,如下所示.
Your statement parses to humans as follows.
回顯由以下組成的連接字符串:
Echo a concatenated string composed of:
- 函數
print('3')
的結果,返回true,字符串化為1
- 字符串'2'
- 函數
print('4')
的結果,返回true,字符串化為1
- The result of the function
print('3')
, which will return true, which gets stringified to1
- The string '2'
- The result of the function
print('4')
, which will return true, which gets stringified to1
現在,這里的操作順序真的很有趣,根本不能以43211
結尾!讓我們嘗試一個變體來找出問題所在.
Now, the order of operations is really funny here, that can't end up with 43211
at all! Let's try a variant to figure out what's going wrong.
echo '1' . print('2') . '3' . print('4') . '5';
這產生 4523111
PHP 將其解析為:
echo '1' . (print('2' . '3')) . (print('4' . '5'));
賓果游戲!左邊的 print
首先被評估,打印 '45'
,這給我們留下了
Bingo! The print
on the left get evaluated first, printing '45'
, which leaves us
echo '1' . (print('2' . '3')) . '1';
然后左邊的 print
被計算,所以我們現在打印了 '4523'
,剩下
Then the left print
gets evaluated, so we've now printed '4523'
, leaving us with
echo '1' . '1' . '1';
成功.4523111
.
讓我們分解一下你對怪異的陳述.
Let's break down your statement of weirdness.
echo print('3') . '2' . print('4');
這將首先打印 '4'
,留給我們
This will print the '4'
first, leaving us with
echo print('3' . '2' . '1');
然后計算下一個打印語句,這意味著我們現在已經打印了'4321'
,剩下
Then the next print statement is evaluated, which means we've now printed '4321'
, leaving us with
echo '1';
因此,43211
.
我強烈建議不要回顯
print
的結果,也不要print
的結果回聲
.這樣做是非常荒謬的.
I would highly suggest not echo
ing the result of a print
, nor print
ing the results of an echo
. Doing so is highly nonsensical to begin with.
經過進一步審查,我實際上并不完全確定 PHP 如何解析這些廢話中的任何一個.我不會再去想它了,它會傷到我的大腦.
Upon further review, I'm actually not entirely sure how PHP is parsing either of these bits of nonsense. I'm not going to think about it any further, it hurts my brain.
這篇關于奇怪的回聲,PHP 中的打印行為?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!