Zend Opcache in PHP improves your page speed by compiling code into machine code and storing the byte code in RAM. With each subsequent hit to a resource code is served from memory rather than being compiled again.
PHP is an interpreted language. What this means is that unlike languages like C / C++, PHP is not compiled once to machine code and then executed from the binary each time.
With PHP, each time a page is viewed on your site the PHP engine parses your source code, compiles the source code to byte code and then executes that byte code.
This is obviously time consuming. To read, parse and compile the same file time and time again seems somewhat insane.
Because reading, parsing and compiling code each time a PHP file is requested is a laborious and time consuming process, there are various opcode caches available. Since PHP 5.5, Zend released their own Zend Opcache as part of the default PHP installation, so that’s the one we use.
When a page is requested the opcode cache is first checked to see if this page’s byte code is in memory and is still valid (fresh). If it is, the byte code is executed directly from memory, which makes it super quick and efficient. The PHP file is not read, parsed or compiled!
Zend Opcache Configuration
As mentioned, since PHP 5.5+ the Zend Opcache is already included in PHP. It is NOT enabled by default, so you’ll need to turn it on and set some parameters in the PHP.ini file.
This is what I use on some of my servers. The exact numbers may differ depending on how much RAM you have, etc, but I use these settings even with as little as 2Gb RAM on servers which are purely nginx web servers with a page cache and cloudflare in front.
[snippet slug=php-opcache lang=ini]
Difference between opcode caching and page caching
Page is caching is when you cache the output of the page. Opcaching is when you cache the underlying PHP code. Ok, this is probably easier to explain with an example.
Lets assume you have a WordPress site for movie reviews. You’re site’s urls look like this this:
If you have no caching at all (page or opcode) then each time someone visits your page with the “Terminal” review the PHP engine parses the code file, compiles the byte code and then generates the output. This happens EVERY.SINGLE.TIME…
This slow process happens for each page view on any of those pages. So, you decide to install a page cache (I use W3 total cache because it works really nicely with nginx).
Now, when someone visits the “Terminal” page the whole process (read PHP file, parse, compile and generate content) takes place. However, with your page cache, the next time someone visits the “Terminal” page, the page is served from cache, so we save a bunch of steps.
Great, so why do we still need an opcode cache? Because that efficiency is only for the “Terminal” page. If someone now comes along and visits the “The Notebook” page, the whole process is completed for that page.
If they then visit the “Titanic” page, the whole processes is completed for that page.
If you have thousands of pages, with page caches going stale and needing to be recached, we still have a LOT of PHP reading, parsing and compiling code.
With an opcache things are different. The first time someone visits any one of those pages, the PHP code is cached. Now when they visit another page, the page’s output itself still needs to be cached (page cache), but a lot of time is saved because the PHP read, parse, compile steps are already in cache..
So, over your thousands of pages the PHP code is only compiled a single time, then used from memory.
Monitoring Opcode Performance
You may want to see how your opcache is performing. For instance, one of the settings is your memory consumption. Have you allocated enough, or are pages being dropped out of the cache because there’s not enough memory?
Fortunately there are a variety of tools to aid with this. A very nice looking tool can be found on Github: OpCacheGUI
Questions / Comment / Suggestions
I’d love to hear your feedback on opcache performance and settings. Please leave a comment below.
John is a freelance Full Stack Developer. Experienced in PHP, Laravel and Vue3/Nuxt3 he can code anything you need.
Based in South Africa, I am available for PHP, Laravel or Vue3/Nuxt3 freelance jobs in person or remotely.