Re: $i++ problem understanding
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.php archive

Re: $i++ problem understanding

From: Hilarion <hilarion@SPAM.op.SMIECI.pl>
Date: Wed Oct 05 2005 - 15:07:44 CEST

>> But much harder to accept is and I fail to see the logic in it,
>> is the following :
>> $i = 1;
>> print($i++);
>> This will print 1 and only afterward will the value of $i be increased
>> by 1. It is between (), so logic tells me first $i++ and then print.
>> [snip]
>
> print is a language construct, not actually a function. That means that
> the parentheses are optional.
> [snip]

My two cents:
Post-increment operator does incrementation after the whole instruction
(which contains it) is executed, not after the expression containing it
is executed, so it does not matter if you are using some language construct
like "print" or "echo" or some function or expressions with different
operator priority and parentheses.

Example:

some_function(($i++ * 2) + 4) | 7);

is equivalent to:

some_function(($i * 2) + 4) | 7); $i += 1;

Example for pre-increment operator:

some_function((++$i * 2) + 4) | 7);

is equivalent to:

$i += 1; some_function(($i * 2) + 4) | 7);

Hilarion
Received on Tue Oct 18 02:32:12 2005