How Does PHP’s Zend Engine Execute Your Code? Inside the Virtual Machine

This article explains how PHP, as a scripted language, is compiled to intermediate code and run by the Zend virtual machine, detailing the VM’s role, JIT compilation, execution stacks, php‑fpm workflow, and key questions about OpCache and configuration loading.

21CTO
21CTO
21CTO
How Does PHP’s Zend Engine Execute Your Code? Inside the Virtual Machine

PHP is a high‑level scripting language that first translates its source code into intermediate code, which is then compiled to native machine instructions for execution by the CPU. The language itself is implemented in C.

A virtual machine (VM) provides an abstract, OS‑independent runtime environment, often called a Process VM or Managed Runtime Environment (MRE). It runs as an application within the operating system, supporting multi‑process, multi‑thread, and security features.

The VM manages the lifecycle of processes—creation, startup, execution, shutdown, and destruction—offering a platform‑independent programming environment that abstracts hardware and OS details, allowing programs to run unchanged on any system.

Like other interpreted languages (Java, Python, C#, Ruby, Lua, Perl, JavaScript, etc.), PHP relies on a VM to hide low‑level OS specifics and focus on solving web‑related problems.

To improve performance, VMs employ Just‑In‑Time (JIT) compilation, defining a subset of instructions that run directly on the host CPU. PHP’s VM, the Zend Engine, consists of two main parts:

Compile stack: converts PHP code into intermediate bytecode.

Execute stack: runs the bytecode using a specialized engine written in C or assembly.

The following diagram illustrates PHP’s execution process:

PHP execution flow diagram
PHP execution flow diagram

The diagram details five steps of PHP execution. Using php‑fpm as an example, the workflow is:

When fpm starts, it runs module_startup and remains resident.

Upon receiving a request, request_startup initializes the request, then execute_script runs the code, followed by request_shutdown which flushes the result and performs cleanup.

When fpm is stopped or reloaded, module_shutdown is executed.

Consider these questions:

At which stage does OpCache operate, and what problem does it solve?

When is the php.ini file loaded, and does each request require parsing?

During a fatal error, the register_shutdown_function callback is triggered—at which step does this occur?

After this callback, does the fpm process continue running?

Feel free to leave comments, and see you next time.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPvirtual machinephp-fpmZend engine
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.