奇怪的回声,PHP 中的打印行为?

Strange echo, print behaviour in PHP?(奇怪的回声,PHP 中的打印行为?)
本文介绍了奇怪的回声,PHP 中的打印行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

下面的代码输出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 将其解析为:

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 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 中的打印行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)