Is PHP compiled or interpreted?
PHP is compiled to opcodes, then run by a virtual machine. PHP compiles each script to opcodes, and the Zend Engine runs them.
How it runs your code
The opcode cache stores the compiled opcodes, so PHP skips the compile on later requests. PHP 8 adds a JIT in two modes, called tracing and function. Both are part of the opcode cache, and both are off by default.
Does that make PHP slow?
The tracing JIT roughly halved our times. It cut the class code from 929 to 446 ns and the plain functions from 579 to 302 ns. Java with the full JIT is still about 11 times faster than PHP with the tracing JIT on the class code.
What we measured
One small program, an order with items, a coupon, a discount and tax, written as an average programmer would write it. Times are nanoseconds per call.
| Setting | Class code | Plain functions |
|---|---|---|
| No opcode cache | 929 | 579 |
| Opcode cache | 866 | 505 |
| Opcode cache and tracing JIT | 446 | 302 |
| Opcode cache and function JIT | 612 | 329 |
Each figure is the median of three runs of the program's own timing loop: 1,000 iterations between two clock reads, median of 200 batches, on PHP 8.5.4.
Questions
Is PHP interpreted or compiled?
PHP compiles each script to opcodes before it runs, and the Zend Engine executes them. The opcode cache saves the compiled opcodes.
What is OPcache?
OPcache keeps compiled PHP opcodes in memory so PHP does not compile a script again on every request.
Does PHP have a JIT?
Yes. PHP 8 added a JIT inside OPcache, with tracing and function modes. It is off by default.
Related
Is Ruby compiled? · Is Python compiled? · Is JavaScript compiled? · Compiled vs interpreted · Why optimization decides speed