开发者

Variable merging which doesn't make sense

Consider this code:

$x  = 1.4;
$i1 = 0.5开发者_Python百科;
$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? Why use this 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.


that would be $i1 + the assignment.

The assignment evaluates to $x ($i2 = $x )

the end result is echo 0.5 + 1.4.

Even php has operator priorities http://php.net/manual/en/language.operators.precedence.php.


= is treated before +, which means that this is what happens:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜