Back to Top

PHP Code Execution Flow

Updated 1 March 2023

Introduction:

  • PHP stands for Hypertext PreProcessor.
  • It is the most extensively used open-source and general-purpose server-side scripting language.
  • PHP is substantially used in web development to produce dynamic websites and applications.
  • It is also used to make numerous Content Management Systems ( CMS ) like WordPress, WooCommerce, etc.
  • The PHP file should be saved with the extension “.php”. For more information go to this.

The Step-By-Step Procedure to run a PHP file on Ubuntu:

To run a PHP application on Ubuntu system we need to install the following software:

  • Apache2 Server
  • PHP
  • MySQLphpMyAdmin

Apache2 server Installation:

We can install the Apache2 server by using the below command:

sudo apt-get install apache2

PHP Installation:

Execute the following command in the terminal to install the latest version of PHP:

sudo apt install php

MySQL Installation:

If we are working with a database then we require a database server on our computer. MySQL is the most popular database with PHP. It can be set up using the below command:

sudo apt-get install mysql-server mysql-client
sudo mysql_secure_installation
Enter current password for root (enter for none): (Press Enter)
Set root password? [Y/n]: Y
New password: (Enter password for MySQL Database)
Re-enter new password: (Repeat password)
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y

phpMyAdmin Installation:

The purpose of the installation of phpMyAdmin is to handle the administration of MySQL database over the web. phpMyAdmin is a graphical user interface for running MySQL.

Searching for an experienced
WordPress Company ?
Find out More

We can install it by using the below command:

sudo apt-get install phpmyadmin

After Installation, let’s move ahead to see how to run a PHP script on Ubuntu system.

Now, to run a PHP script:

1. Go to “Home/www/html” and inside it, we create a folder named “Test”. It’s good practice to create a new folder for every project you work on.

Test Directory.

2. Inside the Test folder, we create a new file named “index.php” and write the following script.

<?php
    echo "Welcome to My Blog!!";
?>

3. Next, open Terminal and type the following command to restart the Apache2 server.

sudo /etc/init.d/apache2 restart

4. After this, we can see the output of the PHP file on the browser. For this, go to a browser and in the address bar, type “localhost/Test/” and now we can see the below output:

The output image of PHP program.

The Flow of PHP Code Execution from Server to Browser:

  • For the execution of PHP files, we have an interpreter called “PHP Interpreter”. It is based on the Zend engine. Zend engine compiled the PHP Script into Zend Opcodes.
  • Opcodes are short for Operation codes. It is low-level binary instructions.
  • These Opcodes are executed and HTML is sent to the client i.e browser.
  • We need HTTP Protocol to accept the request and send the response to the browser.

The following diagram will help you better to understand how the PHP code actually executes till the browser:

PHP Code Execution.

The PHP Interpreter undergoes the following four phases:

  • Lexing
  • Parsing
  • Compilation
  • Interpretation

1. Lexing:

Lexing is the process of converting the PHP source code into tokens. A token in a named identifier for the value.

$code = <<<'code'
<?php
$x = 5;
code;

$tokens = token_get_all($code);

foreach ($tokens as $token) {
    if (is_array($token)) {
        echo "Line {$token[2]}: ", token_name($token[0]), " ('{$token[1]}')", PHP_EOL;
    } else {
        var_dump($token);
    }
}

Output:

Output of Lexing.

From the above output, we can understand that not all pieces of the source code are tokens. Except this, some symbols are named tokens ( i.e. =, ;, :?, etc).

2. Parsing:

Parsing is the process of recognizing tokens within a data instance and looking for recognizable patterns. The parser takes the tokens from the lexer as input and it has two tasks. First, it validates the token, and second, the parser produces the Abstract Syntax Tree( AST ) i.e a tree representation of the source code.

$code = <<<'code'
<?php
$x = 1;
code;

print_r(ast\parse_code($code, 30));

Output:

PHP Parsing.

3. Compilation:

In this phase, the Abstract Syntax Tree( AST ) will be converted into Opcodes. Also, Optimization is performed here (such as resolving some function calls, etc ).

Create a new file suppose “myfile.php” and write the following code in this file.

if ( '7.1.0-dev' ===  PHP_VERSION) {
    echo 'Hii', PHP_EOL;
}

Execute the following command:

php -dopcache.enable_cli=1 -dopcache.optimization_level=0 -dvld.active=1 -dvld.execute=0 myfile.php

After executing the above command, we can see the below output:

PHP Compilation.

4. Interpretation:

Here, the opcodes are run on the Zend Engine and HTML is generated as output. This output will show on the browser.

If you need custom WordPress Development services then feel free to reach us.

!!Have a Great Day Ahead!!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home

Table of Content