OPcache for PHP
How OPcache Keeps Compiled PHP Code in Memory
What is OPcache? It is a caching extension built into PHP that stores the compiled form of scripts in shared memory. Once a script is stored, later requests can run the stored version directly instead of translating the source code again.
Before PHP runs a file, it parses the source code and compiles it into a set of instructions called opcodes, also known as bytecode. Without an opcode cache, this compile step happens on every request, even when the file has not changed. For large applications with many files, compiling can take a noticeable share of each page's total time. OPcache removes most of that cost.
Because the cache lives in shared memory, all PHP worker processes on the server can use the same stored copy. OPcache is one form of server-side caching, and it is often one of the simplest settings to make a PHP host faster.
What Happens When Files Change or Memory Fills
OPcache can check whether a source file has changed, usually by comparing timestamps at intervals. This lets edits take effect without a manual step. Some production servers turn these checks off for speed, which means code changes only appear after the cache is reset or the service reloads. Deployment plans should account for this.
OPcache also has a memory limit. When the cache is full, new scripts may no longer be stored, and the cost of compiling returns for those files. A large codebase may need more memory than the default to keep everything cached.
Frequently asked questions
Does OPcache Speed Up Every Page?
It speeds up every PHP request, because the compile step is skipped for all of them. It does not store page output, though. The application still runs, queries the database and builds the HTML. The gain is largest for code-heavy sites. On a site where a page cache serves most visits, most requests never reach PHP, so OPcache matters less for those visits. The different layers are compared in types of caching.
Is OPcache Enabled by Default?
It depends on the PHP build and the host's configuration, so it is worth checking rather than assuming. Many hosting environments enable it for web requests, while command-line settings may differ. A status page that lists PHP settings shows whether it is active and how much memory it has. Memory size and timestamp checks are set separately.