奇怪的回声,PHP中的打印行为? [英] Strange echo, print behaviour in PHP?

查看:65
本文介绍了奇怪的回声,PHP中的打印行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码输出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:

  1. 函数print('3')的结果,该结果将返回true,并将其字符串化为1
  2. 字符串'2'
  3. 函数print('4')的结果,该结果将返回true,并将其字符串化为1
  1. The result of the function print('3'), which will return true, which gets stringified to 1
  2. The string '2'
  3. The result of the function print('4'), which will return true, which gets stringified to 1

现在,操作顺序在这里真的很有趣,根本不能以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正在将其解析为:

PHP is parsing that, then, as:

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',留下

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.

我强烈建议不要echo给出print的结果,也不要print给出echo的结果.首先,这样做是毫无意义的.

I would highly suggest not echoing the result of a print, nor printing 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中的打印行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆