I was just playing around with PHP when this happened. Look at the commented code.
<?php
error_reporting(0); //turns of errors and notices (which will be shown otherwise)
//here we would've gotten a notice saying $_ has no value, which is true. But PHP automatically gives it the value 0, then we add one to it. ($_++), add $_ and add $_. So it's 1 + 1 + 1 which is two somehow
echo ($_++ + $_ + $_);
So my question is... why does it output 2?
Most of the answer is contained in your code.
$_
is 0
initially. By $_++
, you increment $_
, setting it to one. Therefore, $_
is 1
, but the value of the postincrement (!) $_++
is still 0. The value of ++$_
would be 1.
Then, you add two times $_
(which is 1
), yielding 2 overall.
See this SO post for a detailed preincrement/postincrement comparison.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments