In this chapter
We'll take a first look at what makes PHP fast: how PHP compiles your code into bytecode (opcodes), how OPcache keeps that compiled code in memory for every request — and why that kept the old price on the website after the file was changed — what the JIT adds, and where PHP's runtime settings come from and how to check them.
The Problem in Real Life
Friday, launch rehearsal on the real server. Olivia raises the haircut price: in src/prices.php she changes 2500 to 2600 and uploads the file. She opens the booking page — £25.00. She reloads. £25.00. She checks the file on the server: it says 2600.
An hour later the website still says £25.00. Then Marcus restarts PHP-FPM with one command, and the next page view shows £26.00.
The file says 2600. What is the website reading, if not the file?
Olivia
Compiling Every Time vs. Compiling Once
PHP compiles before it runs
Every request, PHP turns your source code into simple instructions. Doing that again and again is wasted work.
Compiled code in memory
OPcache keeps the compiled instructions in shared memory. Fast — but it may not notice that a file changed.
Which settings are live?
OPcache, JIT, time limits: they all come from php.ini and friends, and you need to know how to check them.
OPcache, Bytecode and JIT: A First Look
What happens before your code runs: this is a first look; Act 23 covers performance in depth and Act 25 the production setup. Four ideas are enough for now:
- Bytecode: what PHP turns your code into.
- OPcache: keeping that compiled code in memory, and when it notices changes.
- JIT: turning hot code into machine code.
- Runtime configuration: where settings come from, and how to check them.
Bytecode and opcodes: a computer can't run PHP source code directly. On every run, PHP first reads the text, checks the grammar, and compiles it into a list of small, simple instructions called opcodes — "add these two values", "call this function", "jump to that line". A list of opcodes is called bytecode. Then PHP's engine, the Zend Engine, executes the opcodes one by one. The diagram below shows the steps. Without help, PHP repeats the compile step for every file on every request — even though the files haven't changed since the last request.
From prices.php to running code — and where OPcache steps in
src/prices.php
source code: text on disk
Compile
read the text, check the grammar, produce opcodes
OPcache (shared memory)
the opcodes are kept for all workers
Zend Engine executes
later requests start here, skipping the compile step
JIT (optional)
very hot opcodes become machine code
OPcache: OPcache is the extension that removes that repetition. The first time a file is compiled, OPcache stores its opcodes in shared memory, which all PHP-FPM workers can read. From then on, every request skips reading and compiling and goes straight to executing. On a real site this often makes PHP several times faster, and it's switched on for the web on most PHP installations (php -v mentions "Zend OPcache" when it's loaded). Two things to remember: OPcache stores code, not data — your variables still vanish at the end of every request (chapter 3) — and on the command line it's usually off (opcache.enable_cli=0), because a single run gains little.
$ php -vPHP 8.5.x (cli) (built: …) (NTS)Copyright (c) The PHP GroupZend Engine v4.5.x, Copyright (c) Zend Technologieswith Zend OPcache v8.5.x, Copyright (c), by Zend Technologies$ php --ri opcache | grep -E "enable|validate"opcache.enable => On => Onopcache.enable_cli => Off => Offopcache.validate_timestamps => On => On
Noticing changed files: so how does OPcache know that prices.php changed? With opcache.validate_timestamps=1, the default, it checks each file's modification time — but at most every opcache.revalidate_freq seconds (2 by default). On a development machine that's fine: you save, and two seconds later you see the change. On production servers, opcache.validate_timestamps is often set to 0: checking is skipped entirely for speed, and OPcache serves the compiled code it has until PHP-FPM is reloaded. That's what Olivia saw. The file on disk said 2600; the website ran the copy in memory, compiled when it said 2500. The fix isn't to change the setting but the deployment routine: after copying new code to the server, reload PHP-FPM — sudo systemctl reload php8.5-fpm — which clears the cache without dropping visitors. Act 25 builds this into every deployment.
; /etc/php/8.5/fpm/conf.d/10-opcache.iniopcache.enable=1opcache.memory_consumption=128opcache.validate_timestamps=0 ; never check files: fastest, but changes need a reload
$ sudo systemctl reload php8.5-fpm # clears OPcache; requests in progress finish normally$ curl -s https://bookbay.example/booking.php | grep HaircutHaircut: £26.00
The JIT: since PHP 8.0, OPcache also contains a JIT compiler ("just in time"). It watches which opcodes run very often and compiles them further, into the processor's own machine code, which runs faster still. It helps most with work that keeps the processor busy — calculations, image processing, long loops. A typical booking page spends most of its time waiting for the database and the network, so the JIT barely changes it. Since PHP 8.4 the JIT is off by default; you switch it on with opcache.jit=tracing. Measure before and after (Act 23); don't switch it on on faith.
; Since PHP 8.4 the JIT is off by default (opcache.jit=disable)opcache.jit=tracingopcache.jit_buffer_size=64M
Runtime configuration: OPcache, the JIT, memory_limit, max_execution_time — they're all runtime settings, and Act 01 showed where they live: php.ini. On a real server they come from several places: the main php.ini, extra files in a conf.d folder (OPcache usually has its own, like 10-opcache.ini), PHP-FPM's pool configuration, and ini_set() in your code for the few settings that can be changed at runtime — OPcache's can't, because they're fixed when PHP starts. To check what's really active: php --ri opcache and php -i on the command line, phpinfo() or ini_get() on the web (remembering that web and CLI can differ). The table below lists the settings from this Act.
| Setting | Typical value | What it does |
|---|---|---|
| opcache.enable | 1 | OPcache on for the web |
| opcache.enable_cli | 0 | OPcache off for the command line |
| opcache.validate_timestamps | 1 in development, often 0 in production | Whether OPcache checks files for changes |
| opcache.revalidate_freq | 2 | Check at most every 2 seconds (when validating) |
| opcache.memory_consumption | 128 (MB) | Shared memory for compiled code |
| opcache.jit | disable (default since PHP 8.4), or tracing | Whether the JIT runs |
| max_execution_time | 30 on the web, 0 on the CLI | Time limit per script, in seconds |
| memory_limit | 128M | Memory limit per request or run |
<?phpforeach (["memory_limit", "max_execution_time", "display_errors", "opcache.enable"] as $setting) {$value = ini_get($setting);echo str_pad($setting, 20), $value === false ? "(not available here)" : $value, "\n";}echo "OPcache: ", function_exists("opcache_get_status") ? "loaded" : "not loaded", "\n";// In the PHP Playground:// memory_limit 128M// max_execution_time 0// display_errors 1// opcache.enable 1// OPcache: loaded
Key Takeaway
PHP compiles source code into opcodes (bytecode) before the Zend Engine runs it. OPcache keeps the compiled opcodes in shared memory so later requests skip compiling — it caches code, not data. With validate_timestamps=0, as on many production servers, changed files are ignored until PHP-FPM is reloaded, so every deployment must reload it. The JIT (off by default since PHP 8.4) compiles hot code to machine code and mostly helps CPU-heavy work. Check the live settings with php --ri opcache, php -i or ini_get().
Why This Matters
OPcache is why PHP can compile your code "on every request" and still be fast: in practice it compiles once and runs the stored result. Knowing that it caches code — and may not look at your files again — explains one of the most common deployment surprises: "I uploaded the fix, but the site still has the bug".
And knowing where settings come from, and how to check the live values, turns "PHP is acting strange on the server" from guesswork into a two-minute check.
After the reload the website shows £26.00, and Olivia adds "reload PHP-FPM" to her launch checklist. Mia can now describe what PHP does from the moment a request arrives to the moment everything is thrown away — and why the terminal is different. One job is left for this Act: put the whole project into files the right way, run it in the browser and the terminal, and trace one request through it. That's the checkpoint.
