RE: Whacky increment/assignment logic with $foo++ vs ++$foo

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> ... 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux