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?


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

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.