简体   繁体   English

变量合并没有意义

[英]Variable merging which doesn't make sense

Consider this code: 考虑以下代码:

$x  = 1.4;
$i1 = 0.5;
$i2 = 0.4;

echo ($i1 + $i2 = $x); // Outputs 1.9

Why is this? 为什么是这样? I've tried searching for this kind of variable setup without results. 我试过没有结果的搜索这种变量设置。 Is the variable $i2 being ignored? 变量$i2被忽略了吗? Why use this over echo ($x + $i1); 为什么要使用这个over echo ($x + $i1); ? It outputs the same result. 它输出相同的结果。

The point is that it does two things in one statement. 关键是它在一个陈述中做了件事。

It is shorthand for: 它是以下的简写:

$i2 = $x;
echo ($i1 + $i2);

The assignment happens inline, saving the separate line. 赋值发生在内联,保存单独的行。 Not ideal style, but often used in if() , while() and other control statements. 不理想的样式,但经常用于if()while()和其他控制语句。

that would be $i1 + the assignment. 这将是$ i1 +任务。

The assignment evaluates to $x ($i2 = $x ) 赋值评估为$ x ($i2 = $x )

the end result is echo 0.5 + 1.4. 最终结果是echo 0.5 + 1.4.

Even php has operator priorities http://php.net/manual/en/language.operators.precedence.php . 甚至php也有运营商优先级http://php.net/manual/en/language.operators.precedence.php

=+之前处理,这意味着发生了这种情况:

echo ($i1 + ($i2 = $x));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM