What Is OPcache? Speeding Up PHP at the Code Level
What Is OPcache?
OPcache is a built-in PHP feature that stores compiled bytecode in server memory. Without it, every request makes PHP re-read your .php files, parse them into bytecode, execute that bytecode, and throw the work away. With OPcache, parsing and compiling happen once; later requests reuse the stored bytecode immediately.
The feature has shipped with PHP since version 5.5 and is usually enabled by default on modern hosting. But "present" and "well configured" are two different things — and its settings are easy to get wrong.
💡 A Simple Analogy:
Think of a foreign-language recipe. Without OPcache, the cook re-reads and re-translates the recipe for every single order. With OPcache, he translates it once and pins the card to the kitchen board — the next order cooks straight from the card. Same dish, far less lead time.
How OPcache Works
The sequence:
- A PHP file is executed for the first time; PHP parses the code into bytecode.
- OPcache stores that bytecode in shared memory, indexed by the file's path.
- Later requests check: is stored bytecode available for this file?
- If yes, and the file hasn't changed, PHP runs the bytecode directly — parsing skipped.
That's why OPcache operates at a different layer from other caches. An object cache stores database results; OPcache stores the translation of your code itself. They sit at different points in the same pipeline, so enabling both isn't an either/or choice — they complement each other.
Why It Matters for Your Website
If you run WordPress, almost all server execution is PHP. Sites with many PHP files that rarely change — plugins, themes — feel the saved parsing time in first-response latency.
On shared hosting and managed platforms, OPcache is already on; you just benefit from it. On a VPS, you configure it yourself — including which PHP version PHP-FPM runs and how much memory OPcache gets. Settings like memory allocation and file-validation intervals suddenly become your responsibility.
A sign O
Pcache isn't active on your server: it doesn't appear in phpinfo(), or the PHP extensions panel in your control panel shows it unchecked. When in doubt, ask hosting support — one short question usually settles it.
Common Mistakes and Tips
Setting validation too long. OPcache can check whether files changed on an interval (every second, every few seconds, or on every request). A very long interval saves a little CPU but delays your code changes from appearing for ages — a classic cause of "I edited the file but nothing changed."
Forgetting to flush after deploying. After uploading new files, clear OPcache via your panel or server command. Stale bytecode still in memory can produce confusing errors.
Allocating too little memory. If the pool is undersized, bytecode gets evicted constantly (cache thrashing) and the benefit evaporates. On multi-app servers, a proper allocation matters more than the CPU saved.
Treating it as a content cache. OPcache stores no finished pages. Content is still built by your application; what's saved is code-preparation work. It doesn't replace page cache or a CDN.
FAQs
Is OPcache always enabled?
Not always. It's common on modern hosts, but custom servers or fresh PHP installs sometimes need it turned on manually. Check phpinfo() or your control panel's PHP settings.
How much does OPcache actually help?
It saves parsing and compile time, not database queries. For applications with many PHP files, the share is substantial. For a site fully served from page cache — where PHP barely runs — the effect is minimal.
OPcache, object cache, page cache — which do I pick?
All three answer different questions and work best as a team. Page cache removes requests from PHP entirely. Object cache speeds up the PHP that still runs. OPcache makes each PHP process itself cheaper.
Do beginners need special settings?
No. Enable it and leave the defaults alone. Touch the configuration only once you see symptoms — slow change propagation after deploys, or odd errors that disappear after a cache clear.
Disclaimer: Hosting Wiki articles are prepared for educational and reference purposes. Hosting technology keeps evolving, so some technical details may change over time.