Does PHP have a virtual machine like Java?
-
4Does PHP have a VM? Sure, it can. Like JVM? Not even close. – L̲̳o̲̳̳n̲̳̳g̲̳̳p̲̳o̲̳̳k̲̳̳e̲̳̳ Aug 01 '10 at 03:52
-
Now there is a VM very similar to the JVM as well. See: https://github.com/Facebook/hiphop-php/wiki – Samuel Lampa Jul 29 '13 at 20:58
3 Answers
Yes.
Independently from the platform PHP is running on, the scripts are compiled into the same bytecode and run by the Zend Engine.
The difference from Java is that this compiled code is usually not stored into separate files and the scripts are re-compiled on each execution (however, see opcode caches).

- 96,375
- 17
- 202
- 225
-
1Are their conceptual differences between the Zend Engine and the JVM? – Christopher Altman Aug 01 '10 at 03:09
-
1@Chris That question is very vague and I don't enough of JVM implementations to even sketch an answer. – Artefacto Aug 01 '10 at 03:11
-
-
You don't know the JVM too much, but do you know a little about the Zend Engine? Like, if it recompiles Zend Engine bytecode to native bytecode? This is potentially one conceptual difference. – zneak Aug 01 '10 at 03:29
-
The major difference is that the JVM deals in byte code from the start after a compilation step, while PHP deals with source code. – user207421 Aug 01 '10 at 04:07
-
2@EJF That difference is not that great once you start adding opcode caches, but we can point one difference: compilation is not all done before the execution of the script, `include` directives, for instance, may trigger compilation after execution has started. – Artefacto Aug 01 '10 at 05:07
-
@zneak - what is *"native bytecode"*? It sound like you are getting your terminology confused here ... – Stephen C Aug 01 '10 at 05:08
-
@EJG Yes, we can't point the lack of JIT compilation. I'd also add lack of multi-threading support, security manager, ... I suppose we could be here for quite a while enumerating things PHP doesn't have :p – Artefacto Aug 01 '10 at 05:08
-
@zneak - well that's what I thought you meant. But I don't think you'll find that anyone else refers to machine code / native code as "native bytecode". Bytecode is called **byte** code for a reason ... and that reason does not apply to typical native instruction sets. – Stephen C Aug 01 '10 at 15:04
-
In addition to the Zend engine: Facebook’s HipHop virtual machine (HHVM), IBM’s WebSphere sMash, Caucho’s Quercus, Phalanger. – Paulius Apr 09 '18 at 16:21
Another important difference between the Zend Engine and a typical JVM is in the way they execute bytecodes:
- The Zend Engine executes (interprets) the compiled bytecodes directly. (At least that's what I think happens. I wasn't able to confirm this from the Zend online documentation!)
- A JVM will normally use a JIT compiler to compile bytecodes to native instructions, and then execute the native instructions.
Actually, JVM behaviour is more complicated than this. JVMs don't always compile to native code, and when they do, they typically delay JIT compilation until they figure it is worth doing. Prior to that, they execute the bytecodes directly.

- 698,415
- 94
- 811
- 1,216
-
2Looking at the source code for Zend shows that it indeed interprets the bytecode directly, rather than converting it to native. And the code isn't pretty either... – siride Aug 01 '10 at 06:13
-
1I had many "what the bleep"-moments when stepping through php source code but frankly the vm elements of the zend engine aren't among those. In comparison to other languages ...well, most of them are like this. – VolkerK Aug 01 '10 at 06:47
Yes, but only recently it is quite like the JVM for Java. It was invented by facebook and announced at the OSCON conference the other week (July 2013).
A news report on the new PHP VM can be read here, and the code and more info can be found on github.

- 4,336
- 5
- 42
- 63