Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incrementing numbers doesn't give an error

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;
like image 615
Daan Avatar asked Nov 28 '25 00:11

Daan


1 Answers

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;
like image 52
CodeCaster Avatar answered Nov 30 '25 15:11

CodeCaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!