PHPBuilder - PHP and Zend Engine Internals
PHPBuilder - PHP and Zend Engine Internals
PHPBuilder - PHP and Zend Engine Internals
ARCHITECTURE
Design
File Formats
Miscellaneous
Object-Oriented
Optimization
Security
Session
Management
Shopping Carts
Style
Transactions
WAP
DATABASES
Any
Database
Interbase
SQL Server
MySQL
ODBC
Oracle
PostgreSQL
FUNCTIONS
Arrays
Aspell
Date
Directory
File
Flash
GD
IMAP
Java
LDAP
Mail
Mathematics
MCAL
Other
Functions
PDF
Regular
Expressions
String
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
1/10
12/23/2016
WDDX
XML
TOOLS
Editors
/IDEs
PEAR
PHPGTK
Frameworks
CODE
Algorithms
BBS
Discussion
CalendarsDates
Databases
File
Management
Games
Graphics
HTML
HTTP
Math
Functions
Money
Networking
Other
Shopping
Carts
Voting
SLIDESHOWS
TIPS
Application
Architecture
Databases
HTML
News
&
Reviews
PHP
Functions
Setup
&
Installation
Site
Operation
Tricks
&
Hacks
Trick
&
Hack
Articles
NEWS
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
2/10
12/23/2016
Application
Architecture
Code
Documentation
Databases
HTML
News
&
Reviews
PEAR
PHP
Functions
PHPGTK
Setup
&
Installation
Site
Operation
Tools
Tricks
&
Hacks
FORUM
CONTRIBUTE
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
3/10
12/23/2016
Articles
Tweet
Like 7
Overview
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
4/10
12/23/2016
Version 1.0 of the Zend Engine functions much like the heart and brain of PHP 4.0. It contains the process that
provides the sub-structure and facilities to the functional modules. It also implements the language syntax, as well.
The Zend Engine 1.0 is actually the second revision of the PHP scripting engine. It is still based on the same rules
as the PHP 3.0 engine that was basically Zend Engine 0.5. Now it is permissible to migrate the path from PHP 3.0
to 4.0. The development has the same state of mind as per PHP 3.0. We feel it is right time to start working
towards a revision of the Zend Engine. It would also incorporate new structures and solutions to some of the most
difcult problems faced by the PHP designer or developers.
In this article I will discuss the Zend Engine internals based on the PHP platform.
Introduction
Zend Engine is an open source scripting engine which acts as an interpreter for the PHP programming language.
This was initially developed by two students at the Technion - Israel Institute of Technology. Zend engine is a
virtual machine or VM. As we know that a Virtual machine is nothing but software which simulates a physical
computer. The Zend engine consists of multiple components e.g. a compiler, ZFMI (Zend Function Module
Interface) and a virtual CPU or an executor.
We know that Zend is a scripting engine and it works as an interpreter. So let us check different phases of a script
which is subjected to a Zend engine. The script passes through the following steps and nally gets executed by
Zend engine:
Step 1: Lexical Analysis - In this step the script is passed through a lexical analyzer, also known as lexer.
Here the script that is human readable is migrated to tokens which are understood and accepted by the machine.
Once the entire script is tokenized, the tokens are passed to the parser.
Step 2: Parsing - In this step, the parser parses the tokens that it received from the lexer and generates an
instruction set which runs on the Zend engine. The Zend engine is nothing but a virtual machine (VM) with an
instruction set, that is similar to assembly language, and executes it. Parser generates the abstract syntax tree that
can be optimized before passing to the code generator. This whole mechanism is jointly called compilation. The
output of the compilation is an intermediate code which is a machine independent code for Zend virtual machine.
This intermediate code contains an array of instruction sets for the Zend Virtual machine, also known as operation
codes or opcodes. These opcodes are three address codes - two operands for the input and one for the output. In
addition to these the opcodes also contain a handler which processes the operands. These opcodes contain
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
5/10
12/23/2016
instructions to perform all sorts of operations ranging from a basic operation on the two inputs and storing the
output onto the third operand to a complex scenario which requires implementing a ow control.
Step 3: Execution - Once the intermediate code is generated, it is passed to the executor which reads each of
the instructions from the array and executes them.
The compilation and execution phases are executed by two separate functions within the Zend engine. These are
Zend_compile and Zend_execute.
ZMFI or Zend Function Module Interface: This interface acts as a communication channel between the
function modules. Function modules are nothing but PHP extensions that have some modules written and included
within them.
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
6/10
12/23/2016
Opcode Cache: Opcode cache is a generic cache which resides within the Zend engine and caches the opcode
of a le. If the le is requested again, it just gets executed from the cache if there is no change in the le.
Some Examples
Let us look at an example to check different phases of a PHP code when it goes through a Zend Engine.
<?php
$name = 'Ricardo';
echo $name;
?>
The above PHP code when subjected to Zend engine, is converted to the following opcode:
The executor of the Zend engine reads these opcodes one at a time and executes it as per the instruction mentioned
in the opcode. The above code is executed in the following manner:
Opnum 0 or Opcode 0 - In this step, the pointer to the variable - 'name' is assigned the Register 0.
Subsequently we use 'ZEND_FETCH_W' (where w stands for write) and assign it to the variable.
Opnum 1 or Opcode 1- In this step, the ZEND_ASSIGN handler assigns the value - 'Ricardo' to Register 0
which is pointer to the variable - 'name'. Register 1 is also assigned but never used. It could have been utilized if we
had an expression such as:
if ($name == 'Ricardo) { }
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
7/10
12/23/2016
Opnum 2 or Opcode 2 - In this step, we re-fetch the value of $name into Register 2. We use the opcode
ZEND_FETCH_R as the variable is used in a read only context.
Opnum 3 or Opcode 3 - In this step, the instruction 'ZEND_ECHO' prints the value of Register 2 by sending
the value to the output buffering system.
Opnum 4 0r Opcode 4 - In this step, the instruction 'ZEND_RETURN' is called which sets the return value of
the script to 1. As we know even if we do not call the explicit return which is true for this case as well, every script
contains an implicit return 1.
<?php
$name = 'Ricardo';
echo strtoupper($name);
?>
As we see here this script initializes a variable and then prints the same after converting the text into upper case.
The intermediate code dump for the above PHP script is quite similar to the earlier one.
The opcodes in the above two examples are quite similar except for the following:
Opnum 3 or Opcode 3 - In this step, the instruction 'ZEND_SEND_VAR' pushes a pointer to Register 2
which has the variable - $name into the stack of arguments. This argument stack is designed to be called by the
functions in the order prints the value of Register 2 by sending the value to the output buffering system.
Opnum 4 0r Opcode 4 - In this step, the instruction 'ZEND_DO_FCALL' is called which internally calls the
'strtoupper' function and also mentions that the output should be send to Register 3.
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
8/10
12/23/2016
Following diagram shows the work ow direction while a PHP script is passes through the Zend engine.
By:alex
Date:1/5/115 0:51
Good article.
Replytothiscomment
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
9/10
12/23/2016
Name/nickname:
Email:
Comment:
PHPBuilder
Policies
Legal Notices
Licensing
Permissions
Privacy
Policy
Advertise
PHP Developer
Topics
PHP Developer
Resources
Architecture
Databases
HTML/CSS/JavaScript
Functions
Tools
E-mail Offers
Forums
Code Snippet
Library
PHP Developer
References
Getting
Started
News
Archive
Setup and
Installation
Site
Operation
Tips and
Quickies
Tricks and
Hacks
http://www.phpbuilder.com/articles/application-architecture/optimization/php-and-zend-engine-internals.html
10/10