In this tutorial, you will learn what PHP is, and how to write your first PHP program with this programming language. Also, I will expose the phases of the Zend Engine, especially the PHP lexical token with an example.

On the other hand, you will learn also the differences between – The interpreter and the compiler, the dynamic web page and the static page.

Also, you will learn how to execute the PHP command on your operating system using the CLI or terminal.

Let’s focus on each section.

PHP is the first and the most scripting language in popularity for webpage development. Also, it can be embedded with HTML markups.

In the next few lines, I’ll explain what PHP means.

An Introduction

PHP is a server-side and interpreted language that has a high performance and power deliverable. There are over 50 million domains around the world that are based on PHP and that make it the first and the most popular web application language.

PHP is a scripting language created to develop interactive and dynamic web pages; this language is written in c++ the high-level programming language.

It is so easy to write a function or a class inside the web pages and then include each other on one page. The server will execute them in a few moments as one snippet without any problem.

Anyway, to write web applications with PHP on your local machine you just need to install a PHP server on your operating system. But before you do that, we need to explain the PHP history and how was the interpreter working.

PHP History Overview

The first release for PHP was in 1994 under the name PHP/FI designed by Rasmus Lerdorf. He wrote this version to monitor and track the visitors who were reading his online resume.

The translator at this time was not known as an interpreter it was only a small program that translates a few letters and numbers.

In the following year, PHP was known as Personal Home Page Tool because the changes were in the translator to read and translate a number of utilities, special instructions and counters for the basic home page – At this time, the parser name was PHP/FI version 2.

The FI was designed in 1995, it stands for Form Interpreter and that was another package that combined with the Personal Home Page Tool to parse the HTML form data.

Then he added another package to allow the parser interacts with the database, its name mSQL.

The PHP/FI was in hot demand in 1997 the total number of websites that were using it was 15000 websites and the number was increasing bit by bit until it reached 50000.

Over the following years, PHP became more popular than any programming language else in web development.

Currently, PHP is using an advanced interpreter designed by the Zend Company, its name – “Zend Engine”.

Superficially, at least you understood some tasks for the old PHP parser in the previous history but the question is, how do the other programming languages work – And here to expose that, I have to explain the difference between the interpreter and the compiler.

Understanding the Differences Between the Interpreter and the Compiler

On either side, the compiler and the interpreter are programs that take the source code or the human language of the high-level programming language and convert it to the binary bits (1s and 0s) – The machine language.

The two both have the same task to generate the binary code which is the machine language to be understood language for the computer.

But there are some differences between them. Let’s understand each one.

The Source Code Executing

The interpreter program translates the source code line by line as one statement. On the other hand, the compiler translates the full source code as one piece.

Code Analyze

Any translator has a thing called lexical analysis to analyze the source code before translating it. So the interpreter doesn’t take time to do the lexical analysis for the source code but the overall time is slower than the compiler.

But the compiler takes a while to do the lexical analysis for the source code and the overall time is faster than the interpreter.

Editing the Source Code during the Executing

As I mentioned, the overall execution in the interpreter is slower than in the compiler but it allows the developers to edit and update their code during the execution process.

On the contrary of the compiler, you will not able to update the source code during the process until you stop the program.

Throwing Error

In the code executing phase, the interpreter will never stop if it has an error in the source code.

On the contrary, the compiler will terminate the executing phase immediately case it found an error in the source code.

The Executing Results

The interpreter doesn’t save the binary bits file in the disk storage. But the compiler already saves this file in the disk storage.


The compiler is affecting the memory more than the interpreter. If the device doesn’t have more efficient components, especially the memory, that will affect the machine’s performance and capabilities.

In the previous section, I explained the differences for each one, but our question is, does the interpreter of the PHP programming language, work as the same in the previous discussion?

The result of this question is yes, and we are going to cover everything related the all stages of the PHP interpreter in-depth.

Let’s dive right in.

How Does the PHP Interpreter Work

As I discussed in the PHP history overview, the interpreter of PHP was a small parser, its task was to read and analyze numbers and some of the small instructions.

In the following years, the interpreter program was developed and currently, it became stronger than its first appearance.

The name of the PHP interpreter is the “Zend Engine”, and here we are going to explore the phases in-depth to see how the interpreter translates the PHP source code with examples.

The first phase is the lexical analysis and its task is, to generate the sequence of tokens and here we will see how the lexical phase analyzes the PHP source code with an example.

Then we will dive more into the following phase, which is called the parser to see the Zend Engine VM tasks.

Also, we will see what is happening in the compilation step to generate the AST. And then the executing stage.

Zend Ending Interpret Steps

So, let’s focus on each one for a few moments.

1. The Lexical Analysis

The duty of this phase is the tokenization or lexing which is to receive the source code from the web server and then delete all whitespaces from the PHP code, remove all comment syntaxes and then convert all characters inside the source code into a sequence of tokens.

Actually, this stage doesn’t produce errors because its mission is lexing or tokenizing. But in general, If the compiler found an error in the source code during this phase, it will terminate this process immediately.

On the contrary of the PHP interpreter, it will throw the error without stopping this phase.

Let’s see how the lexical analysis works on the following PHP example.


   /* This is a php comment syntax */
   $detect = false;
   if ( !$detect ) {
      echo "CodedTag Tutorials"; 


In the previous PHP example, you will see whitespaces and lines in the source code, also there is a PHP comment syntax.

Once the lexical analysis starts, it will remove all unneeded characters like comments, and spaces, and converts the other source code characters to a sequence of tokens.

Before applying the tokenization to the previous PHP code, we just need to clarify the common token names in this phase.

  • The identifier refers to the name of variables or functions which are written by the developers.
  • The comment refers to all comment syntaxes on the source code.
  • The keyword refers to all defined words in PHP or any programming language else.
  • The operator refers to all operators such as division, minus, multiplication, bigger sign, smaller sign and so on.
  • The separator refers to the programming signs such as curly braces, parentheses, brackets, semi-colons and so on.
  • The literal refers to all values that are assigned by the developers like all textual values, numerals, and so on.

Let’s see that with our previous PHP code.

List of Token Names and Values

Firstly, the lexical analysis searches for all the previous common tokens and sets them in a sequence. Like the below table.

Token NameToken Value
literalThis is a php comment syntax
literalCodedTag Tutorials
Table Of Lexical Analysis for PHP Code

Let’s see the final result for the sequence of tokens.

'/*', 'This is a php comment syntax', '*/'
'$', 'detect', '=', 'false', ';'
'if', '(', '!', '$', 'detect', ')', '{'
'echo', 'CodedTag Tutorials', ';'

In the previous section, you saw the final result of the sequence of the lexical tokens. So each line in this sequence contains characters of the main source code and each string has a token name like in the previous table.

Once the lexical analyzer finishes the task, it ignores the comment lines and sends the sequence of tokens to the following phase, which I name the heart of the interpreter. let’s move to the following phase.

2. The Parser

In this phase, the parser receives the sequence of tokens and sends an order to init the Zend Engine VM ( The Virtual Machine ) which has the same job of the assembly language. And according to these VM instructions, the parser starts to manipulate the sequence of lexical tokens that were already been received during the previous phase.

In the previous section, we succeed to generate a sequence of lexical tokens. Now we need to pass these characters to the parser.

Actually, there is another step in the parser, called the compilation to see what is happening there we just need to move into the following stage.

3. The Compilation

As I mentioned before, this phase already works within the parser – Let’s see the compiling process.

In this phase, the parser takes the manipulated lexical tokens and generates the AST ( The Abstract Syntax Tree ) – which is building and performing the structures with nodes, each one has a piece according to the characters that are already displayed in the lexical tokens.

The compilation stage has to pass the AST into the code generator to produce intermediate codes that are known as the OP Codes or Operation Codes which are the machine language ( or the machine code ) – that already depends on the Zend Engine VM.

These OP Codes have three operands, one for the outputs and two for the inputs. These three operands have a list of instructions which are complicated missions that require the execution of the flow control.

4. The Executing

Once the executor receives the OP Codes, it starts to execute and read them according to the list of the array instructions.

The last two phases ( The Compilation and The Executing ) have two direct functions in the Zend engine to do their missions. They are the zend_compile and the zend_execute.

Once the executor is finished with the OP Codes, it produces the final results and it sends them to the web browser.

The following mission of the web browser is rendering them within the HTML markups.

From this point, we are going to explain another concept regarding HTML data rendering, Let’s see the difference between a dynamic web page and a static web page.

The Difference Between the Static Web Page and the Dynamic Web Page

The static web page can only show previously prepared data using HTML structure and it can be changed only if the developer changes the page structure and contents using the HTML development.

On the contrary, the dynamic web page can change the information and contents of its web page according to the incoming data from a database using any programming language which can display different information for different users.

Let’s see how to write a PHP program to see the dynamic web page contents.

How to Write Your First PHP program

In this PHP program, we need to generate unique characters which should contain a group of letters, numbers, and symbols.

Every time you load the page, these characters should be changed which means it shouldn’t be the same value as the previous page load.

But before doing that, you have to make sure that you have installed the PHP package on your operating system. If it doesn’t exist you can install PHP first and go back here again.

The Wamp Server

Currently, I am using a wamp server on my local machine, and my operating system is windows – You can install it through this link,

Accordingly, we just need to know what does mean the Wamp word.

The Wamp server is containing more than one package. Each letter inside the WAMP word refers to a package name. Let’s see how many packages are inside the Wamp Server.

  • The first letter is “W” and this char refers to the Windows.
  • The second letter is “A” and it refers to the Apache Web Server.
  • The third letter is “M” and this char refers to MySQL.
  • The fourth “P” one is referring to PHP.

But where can I find the folder of the public root that will allow me to store all of my PHP projects inside?

Actually, the wamp server only can be installed on the windows operating system, So you will not able to install it with other operating systems.

Create the PHP Program

So the default root is located inside the C drive. and the exact directory of my PHP project should be inside this path C:\wamp\www. Let’s build the PHP project there.

Create a folder and name it – “codedtag” and inside create a file and name it – “index” but make sure the file extension should be with – “.php”.

Now copy past the below PHP code into the “index.php” file and save the changes.


 $all_strings ='1234567890ABCDEF#GHIJKLM$N%O*PQRS-T@&U*VWXYZabcdefghijklmnopqrstuvwxyz';
 $password_array = array(); 
 $collect_len= strlen( $all_strings ) - 1; 
 for ($i = 0; $i < 12; $i++) {
     $numb = rand(0, $collect_len);
     $password_array [] = $all_strings[$numb];
 print(implode($password_array )); 


Open your browser and write localhost/codedtag in the URL. It will show you the generated 12 characters. You can use this script for producing a password. It is including numbers, letters, symbols, and capitals. Every time you load this web page it should change the result.

Generate a Password with PHP

In the previous example, I explained how to display the PHP Program result in the web browser. But some developers like to work directly with the PHP using the CLI ( The Command Line Interface ), So they just need to run some scripts on their operating system without using a web browser.

Let’s execute the previous example using the CLI.

How to Execute the PHP Programming Language with the Command Line Interface

If your operating system is Windows and you are using Xampp or Wamp. And if you need to execute the PHP programming language in CLI, you have to define the PHP folder path into the environment variable by the following steps.

  1. On “My Computer” click on the mouse right click then select properties.
  2. The next step is, to click on the Advanced system setting and then click on the Environment Variables tab
  3. Here you have to copy the PHP version folder path inside the Xampp or Wamp into the path tab.

If your operating system is macOS or Ubuntu just run the command on your terminal directly.

PHP CLI Variable Environment

In the next step, you have to open the CLI – ( click on the Windows start then search for command prompt ). Then navigate to this path C:\wamp\www by writing the following command.

cd /d C:\wamp\www\codedtag

In the next step, we just need to run the PHP command on the index.php file that was already created in the previous PHP program.

php index.php

the result should be printed in the CLI like the below image.

The Result of the PHP Programming Language in the CLI


In this tutorial, you understood what the PHP programming language is, and how to write a PHP program, the difference between the compiler and the interpreter.

Also, we explained the PHP interpreter job ( The Zend Engine ) in clear steps, and how to execute the PHP source code on the CLI or terminal.

For more details visit the PHP manual.