Hacking with PHP has been updated for PHP 7 - click here! >>

Pre-increment and post-increment aren't the same

Take advantage of the fact that PHP allows you to post-increment ($i++) and pre-increment (++$i). The meaning is the same as long as you are not writing anything like $j = $i++, however pre-incrementing is about 10% faster even as of PHP 5.6, which means that switching from post-incrementing to pre-incrementing is perfect for micro-optimisation pedants!

In practice, the difference between the two is so small you wouldn't notice. That said, it's an easy habit to change, so why not?

 

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Don't think that using references will lower your RAM usage >>

Previous chapter: Get your loops right first

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.