Page 1 of 1
why this happens????
Posted: Mon Nov 25, 2002 8:14 am
by yagoda
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;
?>
Posted: Mon Nov 25, 2002 11:57 am
by BDKR
Yeah, this is very strange. They should both equal 15. The behaviour of that first example is odd. I did the same thing in Perl and it worked fine.
Anybody have any other ideas?
Cheers,
BDKR
Posted: Mon Nov 25, 2002 12:18 pm
by BDKR
This is curious too.
Code: Select all
$a = 7;
echo $a = $a + ++$a;
echo "\n";
echo "\$a = $a\n";
//the result should be 16;
Does this make sense to anybody else? Something is severly whacked in Denmark!
Cheers,
BDKR
Posted: Mon Nov 25, 2002 12:31 pm
by 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.
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;
?>
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
Posted: Mon Nov 25, 2002 12:32 pm
by PaTTeR
Posted: Mon Nov 25, 2002 2:34 pm
by BigE
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.
Posted: Mon Nov 25, 2002 3:14 pm
by BigE
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.