Inside PHP: Unveiling the Engine, Extensions, SAPI and Memory Management

This article explores PHP's underlying architecture, covering its design philosophy, four‑layer system (Zend engine, extensions, SAPI, and applications), execution flow with opcodes, core data structures like HashTable, and the internal representation of variables via zval, providing deep insight for backend developers.

21CTO
21CTO
21CTO
Inside PHP: Unveiling the Engine, Extensions, SAPI and Memory Management

1. PHP Design Philosophy and Features

PHP follows a multi‑process model where each request runs in isolation, supports weak typing with runtime type conversion, uses the Zend engine plus extensions to reduce internal coupling, and separates the web server from PHP via a middle‑layer (SAPI). Its simple, flexible syntax makes rapid web development easy, though it can lead to varied coding styles.

2. PHP Four‑Layer Architecture

The core architecture consists of four layers from bottom to top: the Zend engine, Extensions, SAPI, and the upper‑level application.

The Zend engine, written in pure C, parses PHP code into executable opcodes, manages memory, provides core data structures (hashtable, object model) and exposes APIs for extensions.

Extensions implement built‑in functions and libraries and can be custom‑written to add functionality or improve performance.

SAPI (Server Application Programming Interface) decouples PHP from the web server, allowing PHP to run under different environments such as Apache, FastCGI, or CLI.

Upper‑level applications are the PHP scripts that run on top of these layers, analogous to a car’s body on a road.

3. SAPI

SAPI provides hooks for external applications to exchange data with PHP, enabling different execution modes.

apache2handler : runs PHP as an Apache module (most common). cgi : uses the FastCGI protocol, widely adopted by asynchronous servers. cli : command‑line interface for script execution.

4. PHP Execution Process & Opcode

PHP code undergoes lexical and syntactic analysis, is compiled into a series of opcodes, and then executed by the Zend virtual machine. Each opcode consists of two operands, a return value, and a handler function.

ZEND_ASSIGN_SPEC_CV_CV_HANDLER – variable assignment ($a=$b) ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER – function call ZEND_CONCAT_SPEC_CV_CV_HANDLER – string concatenation ($a.$b) ZEND_ADD_SPEC_CV_CONST_HANDLER – addition ($a+2) ZEND_IS_EQUAL_SPEC_CV_CONST – equality check ($a==1) ZEND_IS_IDENTICAL_SPEC_CV_CONST – strict equality check ($a===1)

5. HashTable – Core Data Structure

The Zend HashTable underlies PHP arrays and many internal structures such as symbol tables and global variables. It supports key‑value lookups, can be used as an array, and offers O(1) insertion/deletion.

Supports typical key→value queries Can be used as an array Insertion and deletion are O(1)

Keys may be mixed types (associative and indexed), and values can be of any type, including nested arrays. The hash table combines a hash map with a doubly linked list to enable fast lookups and linear traversal.

6. PHP Variables

PHP variables are weakly typed and stored in a unified structure called zval. A zval contains a type identifier, reference count information, and the actual value (stored as a union to support multiple types).

type – the variable’s data type (int, string, array, etc.) refcount & is_ref – implement reference counting value – the actual data stored

Reference counting enables copy‑on‑write: when a variable is assigned to another, both share the same zval and the count increments; on modification, PHP separates the value to avoid unnecessary copying.

getKeyHashValue h;
index = n & nTableMask;
Bucket *p = arBucket[index];
while (p) {
    if ((p->h == h) && (p->nKeyLength == nKeyLength)) {
        RETURN p->data;
    }
    p = p->next;
}
RETURN FAILURE;

PHP arrays are implemented with HashTable; foreach loops traverse the linked list for high efficiency. Resource variables are represented as opaque pointers (resource IDs) and require registration of custom destructors for proper cleanup.

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.

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