Opcode Caching
Also Known As
opcache
opcode cache
PHP bytecode cache
TL;DR
Storing precompiled PHP bytecode in memory to skip the parse-and-compile phase on subsequent requests.
Explanation
PHP normally parses and compiles source files on every request. Opcode caching (via OPcache, the standard built-in extension since PHP 5.5) stores the compiled bytecode in shared memory so subsequent requests for the same file skip compilation entirely. This can improve throughput by 50–90% for typical applications. The JIT compiler introduced in PHP 8.0 extends this further by compiling hot bytecode to native machine code. Proper OPcache configuration (adequate memory, file count, and deployment cache-clear strategy) is essential in production.
Common Misconception
✗ OPcache only benefits high-traffic sites. OPcache eliminates PHP file parsing and compilation on every request — a 2-5x speedup on most applications regardless of traffic volume. It should be enabled on every production PHP server as a baseline.
Why It Matters
PHP compiles scripts to opcodes on every request without OPcache — OPcache stores compiled bytecode in shared memory, eliminating repeated parsing and compilation that typically consumes 30-50% of PHP execution time.
Common Mistakes
- Not enabling OPcache in production — it is often off by default in minimal PHP installs.
- Setting opcache.validate_timestamps=1 in production — checks file mtimes on every request, negating half the benefit.
- Not setting opcache.memory_consumption large enough — cache evictions cause repeated recompilation.
- Deploying new code without restarting PHP-FPM or touching opcache.php — stale bytecode serves old code.
Code Examples
✗ Vulnerable
; php.ini — OPcache disabled or misconfigured:
;opcache.enable=1 ; Commented out — no caching
opcache.memory_consumption=64 ; Too small for large frameworks — use 256+
opcache.validate_timestamps=1 ; File stat on every request in production — set to 0
✓ Fixed
; php.ini — OPcache production settings
opcache.enable=1
opcache.memory_consumption=256 ; MB — size to your codebase
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0 ; disable in production — manual invalidation
opcache.revalidate_freq=0
opcache.save_comments=1 ; needed for PHPDoc-based tools
; After deploy — invalidate stale cache
; opcache_reset(); or restart PHP-FPM
php -r 'opcache_reset();'
; Or per file: opcache_invalidate('/var/www/app/bootstrap.php', true);
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
15 Mar 2026
Edited
22 Mar 2026
Views
33
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Perplexity 8
Amazonbot 6
Google 4
Ahrefs 3
Unknown AI 1
Also referenced
How they use it
crawler 21
crawler_json 1
Related categories
⚡
DEV INTEL
Tools & Severity
🟠 High
⚙ Fix effort: Low
⚡ Quick Fix
Enable OPcache with validate_timestamps=0 in production (uses inotify or deploy hook to invalidate) — with it enabled your PHP app can serve 5-10x more requests per second
📦 Applies To
PHP 5.5+
web
cli
🔗 Prerequisites
🔍 Detection Hints
opcache.enable=0 in production; opcache.validate_timestamps=1 per-request stat() calls; no opcache warm-up after deployment
Auto-detectable:
✓ Yes
opcache-gui
blackfire
phpinfo
⚠ Related Problems
🤖 AI Agent
Confidence: High
False Positives: Low
✓ Auto-fixable
Fix: Low
Context: File