162

Is PHP compiled or interpreted?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nicky
  • 3,810
  • 9
  • 35
  • 44

10 Answers10

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

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • what do you mean by The binary that lets you interpret PHP is compiled ? – nicky Oct 03 '09 at 20:00
  • 32
    He means the utility called php (or on windows php.exe) is compiled. – sepp2k Oct 03 '09 at 20:02
  • 7
    @nicky It means that the program that's used to interpret PHP is compiled, but the PHP itself is interpreted. – Andrew Song Oct 03 '09 at 20:03
  • 6
    But why its mentioned like this in wikipedia? http://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 '12 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. – Thibault Martin-Lagardette Oct 16 '12 at 09: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 '12 at 18:16
  • Interesting! The language in which most of the web is written today is NOT compiled. – RBT Jul 05 '16 at 00:20
  • interpreted also means slower? – Cris Oct 31 '19 at 18:32
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.

Barry Brown
  • 20,233
  • 15
  • 69
  • 105
  • Variable binding happens at runtime, not compile time. – jrockway Oct 04 '09 at 00:30
  • PHP doesn't even attempt to resolve which names are in scope at compile time? – Barry Brown Oct 04 '09 at 01: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 '21 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 '22 at 05: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.

Andrew P.
  • 161
  • 9
  • 3
    "*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 .. – hagrawal7777 Apr 10 '16 at 02: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 07 '21 at 00: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 '23 at 11:00
17

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

Machavity
  • 30,841
  • 27
  • 92
  • 100
Max
  • 2,293
  • 5
  • 32
  • 48
16

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

code_burgar
  • 12,025
  • 4
  • 35
  • 53
  • 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. – Sedat Kapanoglu Oct 03 '09 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 03 '09 at 20:58
13

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

Claudiu Creanga
  • 8,031
  • 10
  • 71
  • 110
  • 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 '17 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. – Claudiu Creanga Oct 30 '17 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 '22 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.)

outis
  • 75,655
  • 22
  • 151
  • 221
jrockway
  • 42,082
  • 9
  • 61
  • 86
  • 6
    Tokenizing a source code is not compiling. Even VBScript can be regarded as a compiled language with your definition. – Sedat Kapanoglu Oct 03 '09 at 20:13
  • 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 Oct 03 '09 at 20:54
  • 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. – Barry Brown Oct 03 '09 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 03 '09 at 23:25
  • 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 Oct 04 '09 at 00:29
  • 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. – Sedat Kapanoglu Oct 04 '09 at 07:27
  • 1
    I got to this question from 'does php compile to bytecode' in which case I think the difference is relevant. – xenoterracide May 31 '12 at 02:32
  • 3
    I fully agree with Sedat. You can share your knowledge without being a dick to the asker – Alexandre Bourlier Nov 05 '16 at 09:35
  • @jrockway someone did ask your question, verbatim: "Do I pay a performance penalty as PHP recompiles my source code for every request?" but you didn't answer it. – Adam Winter Nov 20 '22 at 04:52
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

Dip
  • 646
  • 6
  • 8
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.

chown
  • 51,908
  • 16
  • 134
  • 170
0

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

RichieHH
  • 2,116
  • 4
  • 28
  • 30