PHP 8 interview question · Most asked
How does the PHP 8 JIT work and when does it improve performance?
Summary
The JIT compiles hot opcodes to native machine code at runtime on top of OPcache. It greatly speeds up CPU-bound work (math, image processing), but gives little benefit to typical I/O-bound web apps.
Longer Answer
PHP normally compiles source to opcodes (cached by OPcache) that the Zend VM interprets. The JIT, built on DynASM, adds a further step: it compiles frequently executed code to native machine code. The tracing JIT (opcache.jit=tracing, default mode when enabled) profiles hot loops and functions; the function JIT compiles whole functions.
Most web requests spend their time waiting on databases, network and I/O, so benchmarks like WordPress or Laravel see small gains. Fractals, ML in PHP and long-running workers benefit far more. PHP 8.4 moved to a new IR-based JIT backend and changed the configuration defaults (opcache.jit=disable with a non-zero buffer).
Code Example
; php.ini
opcache.enable=1
opcache.enable_cli=1
opcache.jit_buffer_size=128M
opcache.jit=tracing
Key Points
- Requires OPcache; enable via
opcache.jit_buffer_sizeandopcache.jit - Tracing JIT targets hot paths
- Large gains for CPU-bound code, small for I/O-bound apps
- Measure with real workloads before enabling in production
Explanation
What interviewers look for
Nuance: why JIT doesn't matter much for CRUD apps, and that OPcache (not JIT) is the big production win.
Common mistakes
- Enabling JIT without OPcache
- Promising 3x speedups for a typical Laravel app
Follow-up questions
- What does OPcache preloading do?
- How would you benchmark JIT impact?
More most-asked PHP 8 questions
- What are the most important features introduced in PHP 8.0?
- What happens to an HTTP request in a PHP-FPM application, and what does 'shared nothing' mean?
- How should passwords be stored in PHP?
- What are named arguments and what are their benefits and risks?
- What is constructor property promotion?
- How does `match` differ from `switch`?
- What are union types in PHP 8 and how do you use them?
- What are enums in PHP 8.1? Explain pure vs backed enums.