Hi all! please try this and see the result:
<?php
$a = 7; echo $a = $a + $a++;
//the result is 14;
$a = 7; $b = &$a; echo $a = $a + $a++;
//the result is 15;
?>
why this happens????
Moderator: General Moderators
This is curious too.
Does this make sense to anybody else? Something is severly whacked in Denmark!
Cheers,
BDKR
Code: Select all
$a = 7;
echo $a = $a + ++$a;
echo "\n";
echo "\$a = $a\n";
//the result should be 16;Cheers,
BDKR
OK,
I found what does work, but the other posted examples should work as well. The logic is just as correct as what is below.
Now it works as it should, but there must be something amiss in the scripting engine that the other logic doesn't work.
Cheers,
BDKR
I found what does work, but the other posted examples should work as well. The logic is just as correct as what is below.
Code: Select all
<?php
$a = 7;
echo $a = $a++ + $a;
echo "<br>";
echo "\$a = $a<br>";
//the result is 15;
$a = 7;
echo $a = ++$a + $a;
echo "<br>";
echo "\$a = $a<br>";
//the result is 16;
?>Cheers,
BDKR
Code: Select all
$a=7;
echo $a += $a++;wow, ok... a little explination is due here I think. $a++ will return 7 and then incrament, so the $a = $a + $a++; should be 14. ++$a incraments then returns the number, so $a = $a + ++$a; would be 15. Why when you reference $a using $b = &$a; then do $a = $a + $a++; returns 15, I'm not sure. Might check in the manual on that one.
OK, I've figgured it out. The reason that after you reference $a that $a + $a++ returns 15 is because when ++ takes precidence. Check out http://www.php.net/manual/sv/language.o ... precedence for more information. Hope that helps.