Is PHP compiled or interpreted?

Php

Php Problem Overview


Is PHP compiled or interpreted?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

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: hhvm.com

Solution 5 - Php

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

Solution 6 - Php

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

Solution 7 - Php

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

Solution 8 - Php

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.

Solution 9 - Php

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

Solution 10 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnickyView Question on Stackoverflow
Solution 1 - PhpThibault Martin-LagardetteView Answer on Stackoverflow
Solution 2 - PhpBarry BrownView Answer on Stackoverflow
Solution 3 - PhpGaurang DeshpandeView Answer on Stackoverflow
Solution 4 - PhpMaxView Answer on Stackoverflow
Solution 5 - Phpcode_burgarView Answer on Stackoverflow
Solution 6 - PhpClaudiu CreangaView Answer on Stackoverflow
Solution 7 - PhpjrockwayView Answer on Stackoverflow
Solution 8 - PhpMagnus AnderssonView Answer on Stackoverflow
Solution 9 - PhpDipView Answer on Stackoverflow
Solution 10 - PhpRichieHHView Answer on Stackoverflow