DEV Community

Cover image for How PHP Works - Behind The Scene
Jamir Hossain
Jamir Hossain

Posted on

How PHP Works - Behind The Scene

PHP (Hypertext Preprocessor) - Overview

PHP is a server-side scripting language initially developed by Rasmus Lerdorf in 1994 to manage his personal website. In 1995, PHP was released to the public, allowing web developers to create dynamic content more easily. PHP code runs on the server side, making it ideal for creating web applications that interact with databases, process user inputs, and deliver dynamic web pages.

Table of Contents

  1. Getting Started with PHP
  2. What are Compiler and Interpreter?
  3. What is an Interpreter, and How Does It Work?
  4. What is a Compiler, and How Does It Work?
  5. Is PHP a compiled or an interpreted language?
  6. How JIT Compiler works in PHP?
  7. Environments to run the PHP code
  8. How does PHP code run through the CLI?
  9. How does PHP code run through a web server?

Getting Started with PHP

Let's start with a simple example:

echo "Hello world";
Enter fullscreen mode Exit fullscreen mode

If you run this code by terminal of your computer, then you will get the output "Hello world" in your terminal. We know that our computer only can understand the machine code like 0 or 1.

  • But how does your computer understand and run the code?
  • How does PHP code convert to a form that the computer can execute?

To understand this, let's look into how computers understand code. The computer only understands machine code, which is a binary format (0s and 1s). For high-level code like PHP to be executed, it needs to be translated to machine code. This is where compilers and interpreters come into play.

What are Compiler and Interpreter

Compiler and Interpreter are both translation software or programs that convert source code written in programming languages into machine code. That is, the code we write, which humans can understand, is converted by these compilers or interpreters into machine code (0s and 1s) that the computer can understand, allowing it to execute instructions and provide us with the output. Let’s dive for basic overview about these translators to understand them better.

What is an Interpreter, and How Does It Work

An Interpreter is a type of translator that reads the entire source code written in a high-level language line-by-line and immediately converts each line into machine code. If it encounters an error while reading a line of code, it stops immediately and reports the error to the programmer, making it easier to debug. This line-by-line execution also makes it a slower process compared to a compiler.

What is a Compiler, and How Does It Work

A Compiler is a type of translator that converts the entire source code written in a high-level language into machine code all at once. If there’s an error in the code, the compiler will still compile the entire code, but the error will be caught at runtime, making it harder for the programmer to identify and fix the error. However, since the compiler converts the entire source code to machine code at once, it generally performs faster than an interpreter during execution.

Is PHP a compiled or an interpreted language

So answer is that, PHP is primarily an interpreted language. When a PHP script is run, the PHP interpreter parses and executes the code at runtime, rather than compiling it to machine code beforehand, as with compiled languages like C or C++. However, some optimizations, like bytecode caching with tools such as OPcache, can improve performance by storing compiled bytecode to avoid reinterpreting the code on every request.

There are also projects like HHVM (HipHop Virtual Machine), which was developed by Facebook to execute PHP code using a Just-In-Time (JIT) compilation process. This allows it to compile PHP code into intermediate bytecode, then compile it to machine code on the fly, making PHP execution faster. With PHP 8, JIT compilation is integrated directly into PHP, allowing parts of the code to be compiled, further blurring the lines between interpreted and compiled behavior.

How JIT Compiler works in PHP

JIT, or Just-In-Time compilation, is a method of executing code by compiling parts of it "just in time" for execution, rather than ahead of time. Unlike traditional interpreted execution, which reads and executes code line by line, JIT compilation translates portions of the code into machine code right before they’re needed during runtime. This results in faster execution because the machine code runs directly on the CPU, avoiding the need for repeated interpretation.

JIT compilation sits between fully interpreted and fully compiled execution. Here’s how it works in general:

  • Initial Interpretation: The code is initially interpreted or run in a lightweight way to analyze which parts are most frequently used or computationally intensive.

  • On-the-fly Compilation: The JIT compiler identifies "hot spots"—code sections that are run often or require optimization. It then compiles these sections into machine code during runtime.

  • Execution: The compiled machine code is stored, so future executions can use the optimized version without recompiling it, speeding up performance.

In PHP, JIT was introduced in PHP 8, allowing frequently used functions or loops to run faster by compiling them into machine code as they’re executed. Other languages, like JavaScript (e.g., V8 engine in Chrome) and Java, also use JIT compilation for similar performance benefits.

Let's follow the below example.

Image description

In the first block, we have our written code. This code first goes into the JIT (Just-In-Time compiler). Then the JIT compiles and executes the code. During this execution, the JIT reads the code line by line and immediately converts each line into machine code after reading it. If there’s an error in any line during the reading process, it stops right there and throws that error.

So, this gives us a bit of an idea about how high-level PHP code (the code we write) is understood and run by the computer.

Environments to run the PHP code

PHP scripts can be run in two main environments: the Command Line Interface (CLI) and a web server environment.

  • Command Line Interface (CLI): PHP code can be executed directly from the command line on your computer. This allows developers to run scripts without a browser, often used for automation, cron jobs, or testing scripts.

  • Web Server Environment: PHP is commonly run within a web server environment, where the code is executed in response to requests from a client/browser. When a PHP script is requested, the web server communicates with the PHP engine through an interface layer (like CGI, FastCGI, or FPM), which processes the script and returns the output to the client.

Each environment serves different use cases, allowing PHP to be a flexible language for both web and command-line applications.

How does PHP code run through the CLI

Our computers contain various applications or programs that we run for different purposes. However, when we go to run these applications or programs, they cannot execute on their own. Instead, they run through the computer's operating system. When we write code in PHP, it also becomes a program that we can run through the computer's terminal. So, when we run our code through the terminal, it also executes via the operating system.

The question, then, is: how does this application or program, or the code we’ve written, communicate with our computer's operating system?

Let's follow the below example.

Image description

Here, the means used to communicate with the operating system is called the OS API (Operating System’s API). Through this OS API, applications or programs can communicate with the computer’s operating system, as shown in the diagram above. This gives us some understanding of how PHP code runs through the terminal on our computer.

How does PHP code run through a web server

First, from the client/browser, we send a request to the web server. However, the web server cannot communicate directly with the PHP engine. For communication between the web server and PHP engine, an intermediate layer works between them, known as SAPI or Server API. Different protocols are used as SAPIs, which allow the web server to communicate with the PHP engine. These protocols include CGI, FastCGI, mod_php, FPM, etc. Any one of these protocols can be used for communication between the web server and PHP engine.

Let's follow the below example.

Image description

The two most popular web servers for running PHP scripts are Apache and Nginx:

  • Apache Server uses the mod_php protocol as its SAPI.
  • Nginx Server uses the FPM protocol as its SAPI.

This gives us a better understanding of how PHP code runs on a web server. Based on the above points.

I hope got a basic idea of how PHP code actually works behind the scenes. If it is helpful, don't forget to share it with others.

Top comments (2)

Collapse
 
tqv_thu_d606129f3ca01d9e7 profile image
tqv thu

Thanks. The article has given me valuable insight into PHP

Collapse
 
jamir_hossain_8800f85fdd5 profile image
Jamir Hossain

Thanks for your valuable comment