views:

155

answers:

2

Does PHP have a virtual machine like Java?

+1  A: 

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).

Artefacto
Are their conceptual differences between the Zend Engine and the JVM?
Christopher Altman
@Chris That question is very vague and I don't enough of JVM implementations to even sketch an answer.
Artefacto
That is fair. Thank you.
Christopher Altman
@Chris You can try to tag this question java to get some attention.
Artefacto
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
The major difference is that the JVM deals in byte code from the start after a compilation step, while PHP deals with source code.
EJP
@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
@zneak - what is *"native bytecode"*? It sound like you are getting your terminology confused here ...
Stephen C
@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
@Stephen C: _native bytecode_ would be _machine code_.
zneak
@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
Where it says 'can't point', 'can point' should be read.
Artefacto
A: 

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.

Stephen C
Looking 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
I 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