Skip to content

The PHP opcache

The PHP opcache is an internal caching mechanism used by PHP. The opcache is enabled in all PHP versions since PHP 5.5.

The opcache speeds up PHP requests by allowing the PHP interpreter to avoid repeatedly parsing and compiling PHP scripts every time a script is used.

Why the opcache exists

PHP is an interpreted language like JavaScript and Python. In an interpreted language, a program called an interpreter is needed to read the code at runtime (that is, to interpret the code). Internally, an interpreter does compile the code into instructions, but it compiles the code into a form of instructions (called operating codes or “opcodes”) that are only meaningful to the interpreter.

Interpreted languages are different from compiled languages such as C and Go. In a compiled language, a program called a compiler converts code into low-level machine instructions that can be executed directly by an operating system.

One of the advantages of interpreted languages is that you do not need to recompile your code every time you make changes to the code. However, there’s a performance cost for interpreted languages: the interpreter needs to read, parse, and internally compile the code at runtime such as when answering a request to your web application.

PHP avoids the performance penalty of recompiling PHP scripts on every request by caching each file’s opcodes. That is, after a PHP file has been read and compiled into opcodes by the PHP interpreter, PHP will cache the file’s opcodes so that PHP does not need to re-read and re-compile every file on every request.