Skip to main content
Shop Support Downloads
IBM Home Products Consulting Industries News About IBM
IBM developerWorks : Web Architecture : Education - Tutorials
Writing efficient PHP
ZIPPDF (letter)PDF (A4)e-mail
Main menuSection menuFeedbackPreviousNext
3. Tweaking PHP code

Using pre-increment versus post-increment page 2 of 11

In PHP, as in C, C++ and Java, you can increment an integer variable by using the special unary operator: ++. For example:

$x = 0;
$x++;    // Post-incrementation. x is now 1
++$x;    // Pre-incrementation.  x is now 2

Similarly, you can decrement a variable by using the unary operator: --.

According to the Zend Optimizer Technical FAQ, pre-incrementation is a faster operation than post-incrementation. This is one of the simpler optimizations performed by the Zend Optimizer (see Resources for details).

Therefore, it is recommended that you use pre-incrementation rather than post-incrementation whenever possible. The only place where this may make a difference is if the incrementation is done as part of an expression evaluation. If it is a stand-alone statement as shown in the preceding examples, you should have no problem changing from post-incrementation to pre-incrementation.

Similarly, you should use pre-decrement instead of post-decrement whenever possible.

Main menuSection menuFeedbackPreviousNext
Privacy Legal Contact