Is PHP code interpreted or compiled ?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

syamswaroop
Forum Newbie
Posts: 19
Joined: Fri May 01, 2009 1:31 am

Is PHP code interpreted or compiled ?

Post by syamswaroop »

hi all,
Iam having a basic doubt that is whether a PHP file is interpreted or compiled ?
and what exactly is preprocessor?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Is PHP code interpreted or compiled ?

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Is PHP code interpreted or compiled ?

Post by Weirdan »

AbraCadaver wrote:PHP is interpreted.
In fact PHP is compiled to bytecode which is executed by zend virtual machine.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Is PHP code interpreted or compiled ?

Post by Jenk »

PHP is interpreted to bytecode.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Is PHP code interpreted or compiled ?

Post by AbraCadaver »

Jenk wrote:PHP is interpreted to bytecode.
Exactly.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Is PHP code interpreted or compiled ?

Post 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'.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Is PHP code interpreted or compiled ?

Post by Eran »

As far as I understand it, compilation is to machine code, interpretation is to bytecode which is an intermediary step before compilation.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Is PHP code interpreted or compiled ?

Post 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.
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Is PHP code interpreted or compiled ?

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Is PHP code interpreted or compiled ?

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Is PHP code interpreted or compiled ?

Post 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)
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Is PHP code interpreted or compiled ?

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Is PHP code interpreted or compiled ?

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Is PHP code interpreted or compiled ?

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Is PHP code interpreted or compiled ?

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