RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo
| From: | Andrea Giammarchi | Date: | Sat, 03 Oct 2009 19:11:37 +0000 |
| Subject: | RE: [PHP] Whacky increment/assignment logic with $foo++ vs ++$foo | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298536@lists.php.net to get a copy of this message | ||
> ... and, in fact, that /is/ how C behaves. The following code:
>
> int a = 2;
> a = a++;
> printf("a = [%d]\n", a);
>
> Will output "a = [3]". At least on Ubuntu 9 using gcc 4.3.3.
>
> So I retract my initial terse reply and apologize for misunderstanding
> your question.
>
> Ben
It's not that difficult to understand ... we are talking about a scripting language as PHP is
The code you wrote for C is not the equivalent while this is:
int a = 2, b;
b = a++;
printf("b = [%d]\n", b);
and b will be exactly 2.
In PHP you are not referencing that variable, you are overwriting variable $a with an integer, 2,
and that's it.
The incremented integer, 3, is simply lost in the silly logic of the operation. The equivalent of
your C code, in PHP, would be just this:
$a = 2;
$a++;
print $a; // of course is 3, the initial $a is not lost
or, to be more explicit ...
$a = 2;
($a = &$a) and $a++;
print $a;
Regards
_________________________________________________________________
Keep your friends updated—even when you’re not signed in.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_5:092010