Page 1 of 4

Is PHP code interpreted or compiled ?

Posted: Sun Feb 21, 2010 9:55 am
by syamswaroop
hi all,
Iam having a basic doubt that is whether a PHP file is interpreted or compiled ?
and what exactly is preprocessor?

Re: Is PHP code interpreted or compiled ?

Posted: Sun Feb 21, 2010 11:18 am
by AbraCadaver
PHP is interpreted. Preprocessor, as in Hypertext Preprocessor, means that PHP is run server side and is processed before the resultant HTML is sent to the client browser.

Re: Is PHP code interpreted or compiled ?

Posted: Sun Feb 21, 2010 2:18 pm
by Weirdan
AbraCadaver wrote:PHP is interpreted.
In fact PHP is compiled to bytecode which is executed by zend virtual machine.

Re: Is PHP code interpreted or compiled ?

Posted: Sun Feb 21, 2010 5:08 pm
by Jenk
PHP is interpreted to bytecode.

Re: Is PHP code interpreted or compiled ?

Posted: Sun Feb 21, 2010 9:56 pm
by AbraCadaver
Jenk wrote:PHP is interpreted to bytecode.
Exactly.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 7:36 am
by Weirdan
Jenk wrote:PHP is interpreted to bytecode.
If you wish to dive into semantics then please define both terms: 'interpreted' and 'compiled'. I'd argue that if bytecode could be stored and reused later directly (without recompilation) it merits calling it 'compiled'.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 7:42 am
by Eran
As far as I understand it, compilation is to machine code, interpretation is to bytecode which is an intermediary step before compilation.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 8:39 am
by Jenk
Weirdan wrote:
Jenk wrote:PHP is interpreted to bytecode.
If you wish to dive into semantics then please define both terms: 'interpreted' and 'compiled'. I'd argue that if bytecode could be stored and reused later directly (without recompilation) it merits calling it 'compiled'.
Unless you specifically implement caching, PHP doesn't do it. Every time a script (file ending with .php or other configured extension) is called, it is parsed and interpreted. There isn't a "build" process for PHP.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 8:56 am
by timWebUK
pytrin wrote:As far as I understand it, compilation is to machine code, interpretation is to bytecode which is an intermediary step before compilation.
Yes you're right.

A comparison would be Java, this is also interpreted into byte code which is then compiled by the Java Virtual Machine into machine code for a particular platform.

PHP certainly isn't compiled.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 9:08 am
by Weirdan
pytrin wrote:As far as I understand it, compilation is to machine code, interpretation is to bytecode which is an intermediary step before compilation.
By my definition compiler is a program that translates from one machine language to some another. The kind of target language is irrelevant, be that machine code, jvm bytecode or C++ (as in HipHop). The difference between interpreter and compiler is that interpreter output cannot be stored for later reuse (due to the reliance on the internal interpreter state for example). Also interpreter would not even attempt to interpret a chunk of code until it needs to execute it.

The problem with the definition of compiler as a program that produces machine code is that many modern CPUs do not execute this code directly but *interpret* it into the set of μops.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 9:19 am
by Eran
As I said, that is the official definition I'm aware of. I understand this is mostly semantics, but as programmers we have a habit of being very detail oriented and so came about this discussion ;)
The CPU is a part of the machine (ie, hardware), so any set of instructions the CPU can read is effectively machine code. In fact, that is the very definition of machine code - http://en.wikipedia.org/wiki/Machine_code (this article goes to great care to differentiate bytecode and machine code)

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 9:59 am
by timWebUK
Modern day CPUs contain microcode/microprograms known as opcodes... I think? Rather than just receiving machine code instructions.

Haven't looked at this low level stuff in a while.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 11:45 am
by Jenk
Weirdan wrote:
pytrin wrote:As far as I understand it, compilation is to machine code, interpretation is to bytecode which is an intermediary step before compilation.
By my definition compiler is a program that translates from one machine language to some another. The kind of target language is irrelevant, be that machine code, jvm bytecode or C++ (as in HipHop). The difference between interpreter and compiler is that interpreter output cannot be stored for later reuse (due to the reliance on the internal interpreter state for example). Also interpreter would not even attempt to interpret a chunk of code until it needs to execute it.

The problem with the definition of compiler as a program that produces machine code is that many modern CPUs do not execute this code directly but *interpret* it into the set of μops.
The definition used to depict the difference between an interpreted language, such as PHP, and a compiled language, such as C, is that you compile C once, and run it many times. PHP is compiled (interpreted) every time it is run.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 11:54 am
by pickle
Jenk wrote:The definition used to depict the difference between an interpreted language, such as PHP, and a compiled language, such as C, is that you compile C once, and run it many times. PHP is compiled (interpreted) every time it is run.
That's the dividing line I've always used. "Compiled" converts the initial language into an intermediate language - once. That intermediate file is then run multiple times. "Interpreted" converts the initial language into an intermediate language each time the program is run.

Re: Is PHP code interpreted or compiled ?

Posted: Mon Feb 22, 2010 3:03 pm
by Weirdan
pickle wrote:That's the dividing line I've always used. "Compiled" converts the initial language into an intermediate language - once. That intermediate file is then run multiple times. "Interpreted" converts the initial language into an intermediate language each time the program is run.
Jenk wrote:The definition used to depict the difference between an interpreted language, such as PHP, and a compiled language, such as C, is that you compile C once, and run it many times. PHP is compiled (interpreted) every time it is run.
Not necessarily. With opcode cache source code is compiled to bytecode once and is run many times. With bcompiler extension you can store this bytecode to disk and run it later (even on different machine).

On the other hand nothing prevents you from writing an interpreter for C (and I'm sure such interpreters exist).