nicky
nicky

Reputation: 3898

Is PHP compiled or interpreted?

Is PHP compiled or interpreted?

Upvotes: 166

Views: 120806

Answers (11)

ALTAMASH
ALTAMASH

Reputation: 26

I know this is an old post and the answer is new. PHP is a compiled language and all the people who say no it is not for them please refer to my answer on this page on github. So, I'll give a short answer here. PHP is a compiled language because it compiles the whole code before executing or showing the result just like JAVA, C++ or C#. As for clever people who say it is both then every language which you guys call compiled is both an interpreter and compiled. Why do you get blue screen, hmmm?

Interpreter definition: An interpreter compiles a code line by line or a single statement in multiple lines and executes it. An interpreter needs a compiler to understand and execute. You can't just say show me the money!

Compiled Langauge Definition: A compiled language is called a compiled language because it compiles the whole code before showing anything. It checks the syntax of the whole application and then produces the result and even after that we get blue screen. So, does that mean all languages are interpreted? The choice is yours, is blue colour a blue colour or is it black. Because both blue and black start with the letter B.

Upvotes: 0

Max
Max

Reputation: 2343

In generally it is interpreted, but some time can use it as compiled and it is really increases performance.

Upvotes: 17

Gaurang Deshpande
Gaurang Deshpande

Reputation: 861

A compiled code can be executed directly by the computer's CPU. That is, the executable code is specified in the CPU's native language.

The code of interpreted languages must be translated at run-time from any format to CPU machine instructions. This translation is done by an interpreter.

It would not be proper to say that a language is interpreted or compiled, because interpretation and compilation are both properties of the implementation of that particular language and not a property of the language as such. So, any language can be compiled or interpreted — it just depends on what the particular implementation that you are using does.

The most widely used PHP implementation is powered by the Zend Engine and is known simply as PHP. The Zend Engine compiles PHP source into a format that it can execute, thus the Zend engine works as an interpreter.

Upvotes: 34

Dip
Dip

Reputation: 696

Just keep in mind, if you need to source code every time to run the program, it means it is using Interpreter. So its an interpreted language.

On the other hand, if you compiled the source code and generate a compiled code which you can executed, then it is using complier. As here you don't need to source code. Like C, JAVA

Upvotes: 4

RichieHH
RichieHH

Reputation: 2123

The accepted answer is blatantly false. PHP IS compiled. End of story. Maybe not to native instructions but to an interpreted bytecode.

Upvotes: 0

PHP is an interpreted language. 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

Upvotes: 142

Claudiu Creanga
Claudiu Creanga

Reputation: 8366

I know this question is old but it's linked all over the place and I think all answers here are incorrect (maybe because they're old).

There is NO such thing as an interpreted language or a compiled language. Any programming language can be interpreted and/or compiled.

First of all a language is just a set of rules, so when we are talking about compilation we refer to specific implementations of that language.

HHVM, for example, is an implementation of PHP. It uses JIT compilation to transform the code to intermediate HipHop bytecode and then translated into machine code. Is it enough to say it is compiled? Some Java implementations (not all) also use JIT. Google's V8 also uses JIT.

Using the old definitions of compiled vs. interpreted does not make sense nowadays.

"Is PHP compiled?" is a non-sensical question given that there are no longer clear and agreed delimiters between what is a compiled language vs an interpreted one.

One possible way to delimit them is (I don't find any meaning in this dichotomy):

compiled languages use Ahead of Time compilation (C, C++);

interpreted languages use Just in Time compilation or no compilation at all (Python, Ruby, PHP, Java).

Upvotes: 14

jrockway
jrockway

Reputation: 42674

This is a meaningless question. PHP uses yacc (bison), just like GCC. 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.

If that doesn't satisfy, consider the following. Both php (the binary) and gcc read your source code and produce an abstract syntax tree. Under versions 4 and 5, php then walks the tree to translate the program to bytecode (the compilation step). You can see the bytecode translated to opcodes (which are analogous to assembly) using the Vulcan Logic Dumper. Finally, php (in particular, the Zend engine) interprets the bytecode. gcc, in comparison, walks the tree and outputs assembly; it can also run assemblers and linkers to finish the process. Calling a program handled by one "interpreted" and another program handled by the other "compiled" is meaningless. After all, programs are both run through a "compiler" with both.

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

Upvotes: 9

code_burgar
code_burgar

Reputation: 12323

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

Upvotes: 17

Magnus Andersson
Magnus Andersson

Reputation: 199

At least it doesn't compile (or should I say optimize) the code as much as one might want it.

This code...

for($i=0;$i<100000000;$i++);
echo $i;

...delays the program equally much each time it is run.

It could have detected that it is a calculation that only needs to be done the first time.

Upvotes: 2

Barry Brown
Barry Brown

Reputation: 20604

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.

Upvotes: 81

Related Questions