简体   繁体   中英

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? 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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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