DEV Community

Cover image for What is PHP and How PHP Interpreter Works
Patricia Hernandez
Patricia Hernandez

Posted on • Updated on

What is PHP and How PHP Interpreter Works

PHP is open-source and depended on the interpreter to parse and execute the source code and get back with the result in a few moments. PHP is the most popular scripting language in web development. And there are some developers using it in machine learning.

An Inroduction

Actually, PHP is designed by Rasmus Lerdorf in 1994 and written in C programming language, starting with the name PHP/FI as the first scripting language on the planet for web development.

PHP started as a common gateway interface has already been created to follow the visitors for their resumes on the internet.

How PHP Works

The interpreting process should start with the browser when the user loads the web page or open any site that interacts with the PHP server.

The client sent a request from the web server during the previous process. The web server takes this source code and interprets the source code line by line. then it responds with the result.

Image description

But how does the interpreter work behind the scene? To answer this question first, you have to read more about what PHP is and try to write your firts PHP program and know the difference between the compiler and the interpreter.

Understanding The Diffrence Between The Compiler and The Interpreter

Two both compiler and interpreter are helping us to translate the source code of the high-level programming language into machine code but there are many factors are distinguish one from the other. Let's see that.

The compiler translates the source code into machine code in one piece, so when you run the program the compiler is taking the full source code and executing it one single time.

In this way, you will not be able to fix errors that may happen during the program execution in the stack.

If your code has an issue with the syntax the compiler will track it right away and show you a message containing the line number of the issue in your editor and also show you what is the problem.

The compiler takes some while to analyze the source code but the overall time is faster in the execution.

The compiler is used in high-level programming languages like C++, C, C# and java.


The interpreter translates the source code line by line which is taking your source code and converting it to binaries 0101 the machine language.

You will be able to rewrite and fix issues during the program execution but there is no issue tracker.

The interpreter is slower than the compiler but it is faster in the source code analysis.

The interpreter is used in high-level programming and scripting languages like JavaScript, PHP, Python and Ruby.

How PHP Interpreter Works

The name of the PHP interpreter is Zend Engine developed by the Zend company for software. The Zend Engine is already inside the PHP package so when you install PHP it will install Zend Engine directly at the same time.

Actually, there are four stages in the Zend Engine interpreter to interpret PHP source code.

1 - Tokenizing Or Lexical Analysis
It takes the source code contents from the PHP programming language. Then converts all the syntaxes into a list of tokens and delete all comments tag and whitespaces from the source code.

2 - Analysis By The Parser
The purpose of this phase is to generate the (AST) structures, in this step, the parser is taking the source code from the tokenizing step and converts it to a big tree

3 - The Compilation
In this phase, the compilation doesn't need a JIT compiler because it already happened in the generated AST. And that according to pass the AST recursively.

4 - The Execution
This is the last step in the PHP interpreter, here the Zend Engine Virtual Machine generates an OPCode that already evaluates the final result for the web response.

What is The AST

AST stands for Abstract Syntax Tree used in the parser of the interpreter. The mission of the AST is to take the syntax of the source code and converts it into a structure tree.

The main advantage of AST is the validating and parsing of every part in the implementation.

Let's see how the abstract syntax tree represents the below PHP code.

  $x = 0;
  while( $x < 10) { 
Enter fullscreen mode Exit fullscreen mode

Image description


In this tutorial, we explained what is PHP, and how the Zend Engine works.
Also, we exposed what is the difference between the compiler and the interpreter.

Thank you for reading



Top comments (0)