tags:

views:

1110

answers:

7

Is php compiled or interpreted?

+12  A: 

The PHP language is interpreted. The binary that lets you interpret PHP is compiled, but what you write is interpreted.

You can see more on the wikipedia page for Interpreted languages

naixn
what do you mean byThe binary that lets you interpret PHP is compiled ?
nicky
He means the utility called php (or on windows php.exe) is compiled.
sepp2k
@nicky It means that the program that's used to interpret PHP is compiled, but the PHP itself is interpreted.
Andrew Song
+7  A: 

Php is an interpreted language. It can be compiled to native bytecode by third party-tools though.

code_burgar
"bytecode" is a term used for VM specific pseudo instructions, which are not native hence can't be regarded as "compilation" in this context.
ssg
"object code" is just bytecode for the CPU's instruction decoder. (You don't think that CPUs actually have native instructions like "CMPSB", right?)
jrockway
A: 

Its interpreted. So keep that in mind when you put too many comments in your code...

Stanislav Palatnik
ROTFL! As a rule we rather put jokes as comments not as answerd, no?
quosoo
This answer would have been true for BASIC.
Barry Brown
Why -3? This answer is true, even for php. The analyzation of whether an instruction is a comment just takes an unsignificant amount of time.
Atmocreations
You take the hit only the first time your script is used.
Barry Brown
For anyone who thinks this is a joke, look at scripts that use heavy commenting throughout the script, and the difference in execution time will be noticable. Ofcourse one comment won't do anything
Stanislav Palatnik
A: 

This is a meaningless question.

PHP uses yacc (bison), just like gcc. Both php and gcc read your source code and produce an abstract syntax tree. PHP then walks the tree to run your program; gcc walks the tree and outputs assembly.

Calling one "interpreted" and the other "compiled" is meaningless. They are both run through a "compiler", after all.

You should actually ask the question you want to ask instead. ("Do I pay a performance penalty as PHP recompiles my source code for every request?", etc.)

(Edit: yacc is a "compiler compiler". The output of yacc is a compiler. The output of a compiler is "compiled". PHP is parsed by the output of yacc. So it is, by definition, compiled.)

jrockway
Tokenizing a source code is not compiling. Even VBScript can be regarded as a compiled language with your definition.
ssg
This means that the word "compiled" is meaningless. Also, lex is the tokenizer; yacc operates on the tokens that flex emits. (Have you ever implemented a programming language?)
jrockway
I'm with jrockway on this, although it doesn't really answer the OP's question. It's tempting to create a taxonomy of languages in which every language is placed cleanly into each category. But the reality isn't so neat. Almost every language is a blend of characteristics. Plus, when you get right down to it, even native machine code is "interpreted" by the processor.
Barry Brown
Your high level assertion is correct, this *is* a meaningless question. Your argument however isn't very helpful, all you've done is taken fuzzy definitions and taken them to extremes. Compilation is the transformation of source into object code. Yes you can consider the transformation of source into a concrete/abstract syntax tree a compilation, but that's not what most people mean is it? Regardless, this is a meaningless question because a language is not *INHERENTLY* compiled or interpreted, implmentations of a language are compiled or interpreted.
Falaina
Is "PHP" a language or an implementation? (For programmers, you all sure have problems defining things clearly. I guess this is what happens when I venture into the PHP section...)
jrockway
let's not oversee that the guy is a beginner. this makes me interpret the question as "does Zend implementation produce native code out of PHP source?". check out my answer. i think you're being too strict about the way the question is asked. you know, we're trying to help, not bash some newbies.
ssg
+8  A: 

In generally it is interpreted, but some time can use it as compiled and it is really increases performance. Open source tool to perform this operation: http://www.phpcompiler.org/

Max
+2  A: 

Both. PHP is compiled down to an intermediate bytecode that is then interpreted by the runtime engine.

The PHP compiler's job is to parse your PHP code and convert it into a form suitable for the runtime engine. Among its tasks:

  • Ignore comments
  • Resolve variables, function names, and so forth and create the symbol table
  • Construct the abstract syntax tree of your program
  • Write the bytecode

Depending on your PHP setup, this step is typically done just once, the first time the script is called. The compiler output is cached to speed up access on subsequent uses. If the script is modified, however, the compilation step is done again.

The runtime engine walks the AST and bytecode when the script is called. The symbol table is used to store the values of variables and provide the bytecode addresses for functions.

This process of compiling to bytecode and interpreting it at runtime is typical for languages that run on some kind of virtual runtime machine including Perl, Java, Ruby, Smalltalk, and others.

Barry Brown
Variable binding happens at runtime, not compile time.
jrockway
PHP doesn't even attempt to resolve which names are in scope at compile time?
Barry Brown
+4  A: 

A definition of a programming language doesn't mandate a "compiled" or "interpreted" form even though one can be much easier to implement based on its design. Theoretically you can write a C interpreter and a Python compiler too.

And "compiled/interpreted" terms are attributed to different things in answers given to this question. What I understand is you're asking if Zend's PHP implementation is compiled into native code or not, and the answer is no. Your PHP code is converted into bytecode and interpreted by an engine, not directly executed by CPU.

But more importantly, it doesn't mean much. You are asking this question because you're curious about one performance point. You want to know if PHP is fast or slow, based on compiled and interpreted status. This is a common mistake done by beginners. For instance, a badly compiled program can be much slower than a finely implemented interpreter. A bad runtime or library can make your native code sluggish enough to be beaten by slowest interpreter.

I assumed that your point was performance because you mentioned you were a beginner but of course you could care about other things too. Native code also makes reverse engineering harder. However I'd like to stay on performance path.

Native code mostly gets useful in computation. When serving web pages if what you do is only a list of system calls (connect to MySQL server, pull data, write to client's socket stream) with small logic in between, the performance gap can actually be small between native and compiled code.

Native is almost always faster but such tradeoff decisions are made to get other benefits from interpretation, such as easier deployment/maintenance, less memory footprint, straightforward cross-platform compatibility, dynamic type system, runtime optimizations etc.

As a final note, there is a PHP compiler called phc.

ssg
much text that can be described in a few sentences. 3rd paragraph: noone ever said that the question was about performance.
Atmocreations
so you didn't read it? here quoting myself for you :) "I assumed that your point was performance because you mentioned you were a beginner but of course you could care about other things too. Native code also makes reverse engineering harder. However I'd like to stay on performance path."
ssg