0

I always see parse or compile in tech articles. So I want to know how PHP works . I found out PHP is Interpreted language, not compiled. I googled online about parse, Interpret and compile, but still not that clear on the whole process. Below is an example PHP code:

<?php
$str1="Hello world!";
$str2="What a nice day!";
echo $str1 . " " . $str2;
?>  

Output:

Hello world! What a nice day! 

Can anyone explain the whole process(parse, interpret...) from source code to the output? thanks

1 Answer 1

6

A very high level and not necessarily complete overview of the process:

  1. The source code is tokenized, i.e. it is broken down into individual parts and these parts are "classified". $str1 is a variable, = is an operator, "Hello world!" is a string literal, ; is a statement terminator etc.
  2. The tokens are transformed into an abstract syntax tree, i.e. the tokens are "grouped by meaning". Something like we have an expression with the = assignment operator whose first operand is $str1 and whose second operand is the string literal "Hello world!".

    These two steps complete the parsing.

  3. The individual parts in the syntax tree are translated into low-level machine instructions, e.g. reserve some memory to store "Hello world!" and create a symbol $str1 to reference it.

    That description is still pretty high level for "machine instructions" BTW and I just use it here to keep it simple.

    This is basically the compilation step.

  4. The instructions are executed.

The distinction between "interpreted" languages and "compiled" languages are somewhat arbitrary. Any language needs to be parsed and then translated into machine instructions, which is basically compilation. "Real" compiled languages must be compiled into a binary executable first which is then manually executed. PHP basically does both in one go, but then throws the executable code away. That's essentially what interpreted code is; it's parsed and compiled on the fly right from the source. There are byte code caches for PHP which cache the machine code, not requiring PHP to re-compile the same code over and over.

"Real" compilers also serve other purposes: they can often detect basic or even pretty complex problems during compilation by analyzing the code and then refuse to compile it; since PHP is winging it as it goes along it cannot catch these kinds of problems during compilation and they will crop up while code is already executing, which requires a different kind of error handling philosophy than compiled languages do. "Real" compilers can also spend more time on optimizing code and potentially make it execute faster; since PHP does it on the fly it doesn't spend much if any time on optimizing code, since this would make it even slower.

BTW, PHP as a language is neither interpreted nor compiled. It's just a language. The standard, official PHP runtime environment is what's interpreting the language. There are also PHP compilers, most famously Facebook's HipHop, which compile PHP code into executable binaries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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