FAQ PHP & Development

What is OPcache and should it be switched on?

OPcache keeps compiled PHP in memory instead of recompiling it on every request, and there is no good reason to leave it switched off.

Updated 4 min read Beginner

OPcache stores the compiled form of your PHP scripts in shared memory, so the server does not have to parse and compile the same files from scratch on every single request. Without it, every visit to a PHP page repeats the same compilation work the previous visit already did — reading the source file from disk, checking its syntax, and turning it into the internal representation PHP actually executes.

Why it makes a real difference

A typical WordPress page load, for instance, involves not one file but dozens — core files, the active theme, every active plugin. Compiling all of that fresh on every request is genuinely wasted work once the code itself has not changed, and OPcache is what stops that waste from happening. Almost every performance recommendation for PHP applications assumes OPcache is on, because so much of what a script does at "compile time" is otherwise repeated needlessly for every visitor.

Should it be on?

Yes, essentially always. There is effectively no downside to leaving OPcache enabled on a normal hosting account, and it is on by default on current PHP builds on most hosting platforms, including here. The one thing worth understanding is what happens when your code changes.

The one thing to know: cached code can lag behind a deploy

A file you just edited may keep running its old cached version briefly

OPcache checks whether a file has changed by comparing timestamps, and depending on configuration it may only do that check periodically rather than on every single request, to avoid the disk-access cost of checking constantly. In practice this means a code change can take a short moment to show up, or — on a setup with file-change checking turned off entirely for maximum performance — may need OPcache cleared manually after a deploy. If a change genuinely will not appear no matter how many times you reload, this is the first thing to check, and it is normal, expected behaviour rather than a fault.

OPcache sits alongside PHP-FPM as one of the two pieces of infrastructure doing quiet, largely invisible work underneath every PHP request — you do not manage either directly on shared hosting, but understanding what they do explains a surprising amount of otherwise confusing behaviour, cached code among it.

Related reading