PHP Intro

Last updated on

In this tutorial, you will learn what the PHP programming language 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 also learn 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 most popular scripting language in terms of webpage development. Also, it can be embedded with HTML markup.

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

An Introduction

PHP is a server-side and interpreted language, but it is also used as a general-purpose programming language. There are over 50 million domains around the world that are based on PHP, which makes it the first and 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 the interpreter was 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 was combined with the Personal Home Page Tool to parse the HTML form data.

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

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

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

Currently, PHP is using an advanced interpreter designed by the Zend Company, whose name is “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. Let’s move onto it.

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 binary bits (1s and 0s)—the machine language.

The two have the same task, which is generating the binary code, which is the machine language to be understood by 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 into 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 execution

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, you will not be 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 there is an error in the source code.

On the contrary, the compiler will terminate the execution phase immediately if it finds an error in the source code.

The Executing Results

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

The CPU

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 well as in the previous discussion?

The answer of this question is yes, and we are going to cover everything related to 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 is stronger than its first appearance.

The name of the PHP interpreter is “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 lexical analysis, and its task is, to generate the sequence of tokens. 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 execution stage.

the php interpreter

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

1. The Lexical Analysis

The duty of this phase is 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, the PHP interpreter will throw an error without stopping this phase.

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

<?php 

   /* 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, and there is also a PHP comment syntax.

Once the lexical analysis starts, it will remove all unneeded characters like comments, and spaces and convert 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 names of variables or functions that are written by the developers.
  • The comment refers to all comment syntaxes in the source code.
  • The keyword refers to all defined words in PHP or any other programming language.
  • 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
comment/**/
literalThis is a php comment syntax
operator$
identifierdetect
operator=
literalfalse
operator;
keywordif
operator(
operator!
operator$
identifierdetect
operator)
operator{
keywordecho
literalCodedTag Tutorials
operator;
operator}

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 from 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 on to the next 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 succeeded in 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 on to the next stage.

3. The Compilation

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

In this phase, the parser takes the manipulated lexical tokens and generates the AST (Abstract Syntax Tree ), which is building and performing the structures with nodes, each of which 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 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 execution) 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 sends them to the web browser.

The following mission of the web browser is to render 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 HTML development.

On the contrary, a dynamic web page can change the information and contents of its 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 have the same value as the previous page.

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 the Wamp word means.

The Wamp server contains 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 character refers to Windows.
  • The second letter is “A” and it refers to the Apache Web Server.
  • The third letter is “M” and this character refers to MySQL.
  • The fourth “P” is referring to PHP.

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

Actually, the wamp server can only be installed on the windows operating system, So you will not be 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 is “.php”.

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

<?php 

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

result-of-hash-password

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.

variables environment on Windows

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

PHP Command Result

Anyway, let’s summarize it.

Wrapping Up

In this tutorial, you understood what PHP is and how its interpreter work behind the scene. You also gained some information about about the PHP history and how it has grown.

So, in the first section, we explain a basic overview of the PHP programming language, which is a popular scripting language for making websites that can work with HTML.

Then, in the following section, you understood what the history of PHP was. And you got some more information about Rasmus Lerdorf who created this language in 1994 to track visits on his online web page.

Then, In the following section, we explained how the interpreter of PHP works, which is the main part of the PHP programming language that reads and understands the PHP code.

To help you learn, the guide includes an example of writing a simple PHP program. This program makes a random string of characters, like a password, every time you load the page. This shows how PHP can make websites interactive and different each time you visit.

Thank you for reading. Happy Coding!

Share on: