PHP engine

Programming languages ​​come in two flavors: interpreted and compiled.
What language is PHP? In order to answer this question, we need to understand the terminology.
A program that translates code written in one programming language into another is called a translator. A compiler is also a translator. It translates high-level language code into machine code. The compilation process creates a binary executable file that can already be run without a compiler.
The interpreter is a completely different category. The interpreter does not translate the code, but executes it. The interpreter analyzes the program code and executes each line of it. Each time you execute such a code, you must use an interpreter.
In terms of performance, interpreters are significantly inferior to compilers, since binary code is much faster. On the other hand, interpreters allow you to fully control the program during its execution.
As far as PHP is concerned, it is neither a compiler nor an interpreter. PHP is a cross between a compiler and an interpreter. Let”s try to figure it out and see how PHP processes the code.

Consider the figure:

Zend API PHP engine technology
We see that PHP is composed of two almost independent blocks – a translator and an interpreter. Why did you need to do this? Of course, for performance reasons.
A script is fed into PHP. It translates (translates) it, checking the syntax, into a special bytecode (internal representation). PHP then executes the bytecode (not the code of the program itself) without creating an executable file.
Bytecode is much more compact than ordinary program code, so it is easier (and faster) to interpret (execute). Judge for yourself: parsing is carried out only once at the translation stage, and the “semi-finished product” is already executed – byte-code, which is much more convenient for these purposes. Therefore, PHP is more of an interpreter than a compiler. This “double work” was necessary for the following purposes.

Consider a loop:

For (i = 0; i <10; i ++) {
This cycle will “spin” 10 times. In each of these ten passes, the interpreter must parse and execute 100 lines of code. As a result, he needs to analyze and execute 10 * 100 = 1000 lines of code! If we translate the entire cycle into bytecode once, then it will have to analyze 10 times less! This means that scripts will run 10 times faster!
It turns out that PHP is a translating interpreter.
The main phase of PHP work is the interpretation of the internal representation of the program and its execution. It is this phase that takes the most time in serious scenarios. However, the slowdown is not that significant.
It is worth remembering that PHP version 3 was a “pure” interpreter, “and since PHP 4, scripts have become much faster to execute, since PHP 4 (and PHP5) is an interpreter translator.
The Perl language, which is almost always called a compiler, works exactly the same way – it translates the text of the program into an internal representation, and then uses the resulting code at execution. So you can say PHP version 4 is as much a compiler as Perl is.
So, we are forced to conclude that PHP is an interpreter with a built-in translation block that optimizes the course of interpretation.

Using an interpreter (and hence PHP) has its undeniable advantages:

  • There is no need to worry about freeing the allocated memory, there is no need to close files when you finish working with them – the interpreter will do all the routine work, since the program is executed under its vigilant control;
  • You don”t need to think about the types of variables, and you also don”t need to declare a variable before using it for the first time;
  • Debugging programs and detecting errors are greatly simplified – the interpreter has complete control over this process;
In the context of web applications, the interpreter also has a very important advantage – there is no danger of the server “freezing” if the program does not work properly.
There are other advantages as well. In general, using an interpreter can give scripts the power that Web users expect from them.
The loss in PHP performance is noticeable in the case of large and complex loops, when processing a large number of lines, etc.
However, note that this is the only PHP drawback that will appear less and less as more powerful processors come out, so that in the end , generally come to naught.

Leave a Comment