Use a PHP code cache for newer PHP versions

If you're using PHP 5.5 or later, there is one simple way you can double the speed of your server, and that is to enable the Zend OPcache extension - it is fast, free, and ships as standard in all new versions of PHP. Previously there were other options, such as the Alternative PHP Cache (APC), but OPcache is now the standard and it's excellent – there's no reason to use anything else for most scenarios.

To enable OPcache, make sure this is set in your php.ini file: opcache.enable=1. You'll need to restart your server to have that setting take effect, and it's worth using phpinfo() to verify that it's enabled correctly. Note that some configurations of OPcache mean that any changes you make to a script won't take affect for a period of time, sometimes up to a minute. This is there to maximise performance, but may be a bit confusing at first!


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: Read the manual carefully >>

Previous chapter: Use the Zend Optimizer for older PHP versions

Jump to:


Home: Table of Contents

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