At 9:21 PM +1000 10/3/09, wrote:
Daevid Vincent is surprised that:

$num = 123;
$num = $num++;
print $num;  //this prints 123 and not 124 ?!!

I can understand why someone might think this is not correct, but they need to understand what is happening and why the above second assignment statement is in bad form (IMO).

The first statement assigns 123 to $num. Everyone is OK with that.
The next statement assigns 123 to $num, but then tries to increment $num, but doesn't because the assignment overrides the ++ operator -- this is bad form (IMO).

Now, if you change the syntax to this:

$num = 123;
$num = ++num;
print $num;            //this prints 124

The first statement assigns 123 to $num.
The next statement adds 1 to $num and then assigns 124 to $num, but it's still not the best form because you can do the same thing with:




It's usually easier to understand if one doesn't compound statements with ++ operator.




PHP General Mailing List (
To unsubscribe, visit:

Reply via email to