162

Is PHP compiled or interpreted?

10 Answers 10

141

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

8
  • 32
    He means the utility called php (or on windows php.exe) is compiled.
    – sepp2k
    Oct 3, 2009 at 20:02
  • 7
    @nicky It means that the program that's used to interpret PHP is compiled, but the PHP itself is interpreted. Oct 3, 2009 at 20:03
  • 6
    But why its mentioned like this in wikipedia? goo.gl/YOwZ Since PHP 4, the PHP parser compiles input to produce bytecode for processing by the Zend Engine, giving improved performance over its interpreter predecessor.
    – kiranvj
    Jul 28, 2012 at 16:17
  • 6
    @kiranvj It is indeed compiled for improved performances, but at runtime. Think of "compiled languages" as "pre-compiled", and interpreted languages as "Compiled when run". It's the difference between these two: - JIT ("Just in time"), where only the code that is needed is compiled when it's needed (note, however, that after the interpreter quits, the compiled code is lot) - AOT ("Ahead of time"), where the all the code is compiled before it is run. Oct 16, 2012 at 9:37
  • 4
    @kiranvj: I believe OP wanted to know if PHP gets compiled to the native code. Compiling it to bytecode, which is then again interpreted by Zend, is somewhere in the middle in terms of compilation cost and runtime performance. Lol, this is a rather old thread, just realized it.
    – vgru
    Dec 10, 2012 at 18:16
77

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.

4
  • Variable binding happens at runtime, not compile time.
    – jrockway
    Oct 4, 2009 at 0:30
  • PHP doesn't even attempt to resolve which names are in scope at compile time? Oct 4, 2009 at 1:04
  • From what I have seen there is ONLY PHP interpreter, also know as the runtime. What is the name of the compiler you refer to, and where can one fined it?
    – Mike
    Dec 27, 2021 at 12:32
  • 1
    I think your description is inaccurate and can lead to confusion, php (compiled php binary is not interpreted by any runtime engine, the php files are).
    – WilliamX
    May 28, 2022 at 5:49
33

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.

3
  • 2
    "That is, the executable code is specified in the CPU's native language" .. Executable code is not specified in CPU's native language but in binary format, CPU's native language could be anything and when software binaries doesn't come like it is for this CPU or that CPU .. A fully compiled and executable code will be in binary format ..
    – hagrawal
    Apr 10, 2016 at 2:44
  • This is one of the best explanations of the matter that I've ever encountered. When I started out in computer programming, it was with HP BASIC, an interpreter, and it was relatively slow. Years later I learned that Dartmouth BASIC, from which all subsequent versions of BASIC were derived, was always compiled from the start and ran much faster on the minicomputer systems of its time.
    – Andrew P.
    Jun 7, 2021 at 0:54
  • That last sentence is interesting: "The Zend Engine compiles PHP source... thus the Zend engine works as an interpreter" 🤔 So it compiles but is not a compiler? In fact the whole distinction interpretator/compiler is getting more and more blured over the years when code gets compiled in a intermediate form which then interpreted by some sort of virtual machine, in this case the Zend Virtual Machine.
    – theking2
    Jan 28 at 11:00
17

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

0
16

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

2
  • 2
    "bytecode" is a term used for VM specific pseudo instructions, which are not native hence can't be regarded as "compilation" in this context. Oct 3, 2009 at 20:12
  • 7
    "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
    Oct 3, 2009 at 20:58
12

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

3
  • Doesn't Java use ahead of time compilation in most cases, though? I'm a fairly new Java developer and we usually compile our code way before runtime.
    – aCarella
    Oct 30, 2017 at 12:37
  • 1
    @aCarella in most cases, yes. in all cases, no. so you cannot draw a line that is semantically significant and agreed. Oct 30, 2017 at 14:43
  • @aCarella Java is special case. Java is compile ahead of time form *.java to so called 'intermediate code' --> *.class. The problem is that the IC is not executable by standard processors. That IC is then interpreted by JVM. The thing is, that the JVM itself is able to do JIT and parts of code can be compiled to the native machine code just in time of execution.
    – svobol13
    Aug 22, 2022 at 20:07
9

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

9
  • 6
    Tokenizing a source code is not compiling. Even VBScript can be regarded as a compiled language with your definition. Oct 3, 2009 at 20:13
  • 5
    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. Oct 3, 2009 at 22:33
  • 4
    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
    Oct 3, 2009 at 23:25
  • 6
    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. Oct 4, 2009 at 7:27
  • 3
    I fully agree with Sedat. You can share your knowledge without being a dick to the asker Nov 5, 2016 at 9:35
3

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

2

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.

1
  • Now we have a option called generators. Feb 5, 2016 at 18:56
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.