增加数字不会产生错误

How come this code below echo's 2 and does not give an error, does it just ignore +1+2+3+4 ?

I've searched but couldn't find an answer.

<?php
$i = 1;
$i+++1+2+3+4;
echo $i;

That line:

$i+++1+2+3+4;

Says:

  • Increment $i
  • Add the value of $i pre increment to +1+2+3+4, but don't store the result anywhere.

Hence $i == 2.

If you wouldn't want it to be ignored, you should store the result:

$i = $i+++1+2+3+4;

All is fine. You just forgot the assignment, so i is affected only by ++ operator:

<?php
$i = 1;
$x = $i+++1+2+3+4;
echo "{$i} vs "{$x}";

would return

2 vs 11

You never assign the completed operation anywhere:

These two are functionally equivalent:

$i++;
$i = $i + 1;

both will increment $i by 1, and save that incremented value in $i

With $i+++1+2+3+4 you're essentially executing

  ($i++) + 1 + 2 + 3 + 4

which is

 $i = $i + 1;
 1 + 2 + 3 + 4; // useless, result not stored anywhere

so which increments $i by 1, saves that to $i, then does the other additions. But since those aren't being saved anywhere, the result is thrown away.

if you had

php > $i = 1;
php > $i = $i+++1+2+3+4;
      ^^^^^----add this
php > echo $i;
11

then it would have worked as you expect.

$i++ means add 1 to $i.
and like python, the +1+2+3+4 means add the value of $i pre increment to +1+2+3+4 but don't store it anywhere.(so no memory address or anything like that...).
so what you get is just $i==2