SlideShare a Scribd company logo
PHP
Under The Hood
What Is PHP?
● An Easy To Learn Language?
○ Sure!
● A Simple Language?
○ Usually
● A Well Designed Language?
○ HA!
● An Effective Language!
○ Absolutely!
PHP, Under The Hood - DPC
PHP Is...
Dynamically
Typed
$a = 1;
$b = "a";
PHP Is...
Weak Typed
1 + "1b" == 2
PHP Is...
Implementation
Defined
Implementations
Main:
C-PHP: www.php.net
Alt:
HipHop - Facebook
Roadsend - Compiler
Phalanger - .NET
PHP Compiler - Compiler
PHPPHP - PHP
PHP, Under The Hood - DPC
Interesting Note:
C-PHP Requires
PHP To Compile!
PHP Is...
Compiled
PHP, Under The Hood - DPC
PHP Is...
Dynamically
Compiled
On-Demand
PHP, Under The Hood - DPC
It All Starts With The
SAPI
SAPI
● Server API
● "Starts" PHP and issues request(s)
● Common SAPIs:
○ CLI
○ CGI (and FastCGI)
○ mod_php
○ FPM
○ etc...
Execution Pipeline
Execution Pipeline
Start Up
Request
Shut Down
SAPI
Execution Pipeline
Start Up
Request
Shut Down
SAPI
Config Init
Engine Init
Module Init
Zend/zend.c
PHPPHP/PHP.php
Execution Pipeline
Start Up
Request
Shut Down
SAPI
Module
Shutdown
Engine
Shutdown
Zend/zend.c
Execution Pipeline
Start Up
Request
Shut Down
SAPI
Request Init
Compile Code
Execute Code
Request
Shutdown
PHP, Under The Hood - DPC
Compiler Pipeline
Lexer
Lexer Step
● C-PHP
○ Tokenizes using RE2C
○ Produces Array of Tokens
● PHPPHP
○ Uses core tokenizer (or emulative)
○ PHPParser Powered
Zend/zend_language_scanner.l
PHPParser/Lexer/Emulative.php
Compiler Pipeline
Lexer
Parser
Parse Step
● C-PHP
○ Directly Generates OPCode Array
○ BISON Powered
● PHPPHP
○ Generates AST Structure
○ PHPParser Powered
Zend/zend_language_parser.y
Compiler Pipeline
Lexer
Parser
Compiler
Compile Step
● C-PHP
○ Compiled Directly During Parse
○ Single Pass Compilation
● PHPPHP
○ Recurses Over AST
○ Single Pass (for now)
○
PHPPHP/Engine/Compiler.php
Compiler Pipeline
Lexer
Parser
Compiler
OpCode
OpCodes
● A Series of Operations
○ Just like Assembler Opcodes
○ Represent "units of functionality"
● Designed to run on Virtual Machine
○ Zend Engine
○ Or PHPPHP!
$a = 1;
$b = 2;
echo $a + $b;
PHP, Under The Hood - DPC
Notice Anything?
PHP, Under The Hood - DPC
PHP, Under The Hood - DPC
What If We
Cached The
OpCodes?
We Can Cache!
● Given the compiler is Idempodent
○ (has no side-effects)
○ (hint: it's not)
● OpCodes are really Pointers
○ Swizzling!!!
In Other Words
OpCode Caching
Is Hard!
PHP, Under The Hood - DPC
Time To Execute!
Zend/zend_vm_execute.h
PHPPHP/Engine/Executor.php
Executor Pipeline
OpCode
Is
Return?No Yes
Return
PHP, Under The Hood - DPC
But What Are We
Executing?
Zend/zend_vm_execute.h
Interesting Note:
vm_execute.h
Is Generated By
PHP
PHPPHP/Engine/OpLines/Add.php
Variables!
Zend/zend.h
PHPPHP/Engine/Zval/Value.php
PHP, Under The Hood - DPC
Ref-Counting
● RefCount + References
○ Allows Copy-On-Write
● Variable Is "Deleted" When
RefCount = 0
● Enables Primitive Garbage
Collection
○ Circular GC is also implemented
That's All There Is
To It!
PHP, Under The Hood - DPC
Let's Look At An
Example
$a = 1;
$b = 2;
var_dump(
$a + $b
);
line # op return operands
-----------------------------------
2 0 ASSIGN !0, 1
3 1 ASSIGN !1, 2
6 2 ADD ~2 !0, !1
3 SEND_VAL ~2
4 DO_FCALL 'var_dump'
5 RETURN 1
[0] => PHPPHPEngineOpLinesAssign
[1] => PHPPHPEngineOpLinesAssign
[2] => PHPPHPEngineOpLinesAdd
[3] => PHPPHPEngineOpLinesInitFCallByName
[4] => PHPPHPEngineOpLinesSend
[5] => PHPPHPEngineOpLinesFunctionCall
[6] => PHPPHPEngineOpLinesReturnOp
[0] => PHPPHPEngineOpLinesAssign Object (
[op1] => PHPPHPEngineZvalPtr Object (
[zval:protected] => PHPPHPEngineZvalVariable Object (
[name:protected] => PHPPHPEngineZvalPtr Object (
[zval:protected] => PHPPHPEngineZvalValue Object (
[value:protected] => a
[refcount:protected] => 1
[isRef:protected] =>
[dtorFunc:protected] =>
)
)
[class:protected] =>
[zval:protected] =>
[executor:protected] =>
[scope] => 1
)
)
[op2] => PHPPHPEngineZvalPtr Object (
[zval:protected] => PHPPHPEngineZvalValue Object (
[value:protected] => 1
[refcount:protected] => 1
[isRef:protected] =>
[dtorFunc:protected] =>
)
)
[result] =>
[lineno] => 2
)
PHPPHP/Engine/OpLines/Assign.php
PHPPHP/Engine/OpLines/Add.php
PHPPHP/Engine/OpLines/InitFCallByName.php
PHPPHP/Engine/OpLines/Send.php
PHPPHP/Engine/OpLines/FunctionCall.php
There's A Ton
More
PHP, Under The Hood - DPC
PHP, Under The Hood - DPC
Get Involved!
PHP, Under The Hood - DPC
More Info
● github.com/php/php-src
● lxr.php.net
● github.com/ircmaxell/PHPPHP
● Reference Series
○ wiki.php.net
○ blog.ircmaxell.com
■ PHP Internals Series
Anthony Ferrara
Joind.in/8443
@ircmaxell
blog.ircmaxell.com
ircmaxell@php.net
youtube.com/ircmaxell

More Related Content

PPTX
php and sapi and zendengine2 and...
do_aki
 
PPTX
PHP と SAPI と ZendEngine3 と
do_aki
 
PPTX
php-src の歩き方
do_aki
 
PDF
Quick tour of PHP from inside
julien pauli
 
PPT
Introduction to Thrift
Dvir Volk
 
PDF
モダン PHP テクニック 12 選 ―PsalmとPHP 8.1で今はこんなこともできる!―
shinjiigarashi
 
PDF
JIT のコードを読んでみた
y-uti
 
PPTX
C#/.NETがやっていること 第二版
信之 岩永
 
php and sapi and zendengine2 and...
do_aki
 
PHP と SAPI と ZendEngine3 と
do_aki
 
php-src の歩き方
do_aki
 
Quick tour of PHP from inside
julien pauli
 
Introduction to Thrift
Dvir Volk
 
モダン PHP テクニック 12 選 ―PsalmとPHP 8.1で今はこんなこともできる!―
shinjiigarashi
 
JIT のコードを読んでみた
y-uti
 
C#/.NETがやっていること 第二版
信之 岩永
 

What's hot (20)

PDF
Akka.NET 으로 만드는 온라인 게임 서버 (NDC2016)
Esun Kim
 
PDF
Asynchronous JavaScript Programming
Haim Michael
 
KEY
Spring AOP
Jeroen Rosenberg
 
PDF
NDC12_Lockless게임서버설계와구현
noerror
 
PPTX
Node.js Express
Eyal Vardi
 
PDF
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
PPTX
소셜게임 서버 개발 관점에서 본 Node.js의 장단점과 대안
Jeongsang Baek
 
PDF
코딩 테스트 및 알고리즘 문제해결 공부 방법 (고려대학교 KUCC, 2022년 4월)
Suhyun Park
 
PPTX
Express js
Manav Prasad
 
PDF
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
Chris Ohk
 
PDF
Web Development with NodeJS
Riza Fahmi
 
PDF
손코딩뇌컴파일눈디버깅을 소개합니다.
Kwangsung Ha
 
PDF
송창규, unity build로 빌드타임 반토막내기, NDC2010
devCAT Studio, NEXON
 
PDF
async/await不要論
bleis tift
 
PDF
メディア・アートII 第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーション
Atsushi Tadokoro
 
PPT
Real Time Communication using Node.js and Socket.io
Mindfire Solutions
 
PDF
PHP-FPM の子プロセス制御方法と設定をおさらいしよう
Shohei Okada
 
PDF
Flask Introduction - Python Meetup
Areski Belaid
 
PPT
Rust Programming Language
Jaeju Kim
 
Akka.NET 으로 만드는 온라인 게임 서버 (NDC2016)
Esun Kim
 
Asynchronous JavaScript Programming
Haim Michael
 
Spring AOP
Jeroen Rosenberg
 
NDC12_Lockless게임서버설계와구현
noerror
 
Node.js Express
Eyal Vardi
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Visual Engineering
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
소셜게임 서버 개발 관점에서 본 Node.js의 장단점과 대안
Jeongsang Baek
 
코딩 테스트 및 알고리즘 문제해결 공부 방법 (고려대학교 KUCC, 2022년 4월)
Suhyun Park
 
Express js
Manav Prasad
 
고려대학교 컴퓨터학과 특강 - 대학생 때 알았더라면 좋았을 것들
Chris Ohk
 
Web Development with NodeJS
Riza Fahmi
 
손코딩뇌컴파일눈디버깅을 소개합니다.
Kwangsung Ha
 
송창규, unity build로 빌드타임 반토막내기, NDC2010
devCAT Studio, NEXON
 
async/await不要論
bleis tift
 
メディア・アートII 第2回 openFrameworks基礎 配列、くりかえし、乱数 ベクトルを使用したアニメーション
Atsushi Tadokoro
 
Real Time Communication using Node.js and Socket.io
Mindfire Solutions
 
PHP-FPM の子プロセス制御方法と設定をおさらいしよう
Shohei Okada
 
Flask Introduction - Python Meetup
Areski Belaid
 
Rust Programming Language
Jaeju Kim
 
Ad

Viewers also liked (14)

PDF
PHP 7 new engine
julien pauli
 
PDF
Being functional in PHP (PHPDay Italy 2016)
David de Boer
 
PPTX
Php
Shyam Khant
 
PPTX
Internet of Things With PHP
Adam Englander
 
PPTX
PHP Optimization
djesch
 
PDF
PHP WTF
markstory
 
PDF
PHP 7 performances from PHP 5
julien pauli
 
PPTX
Php internal architecture
Elizabeth Smith
 
PPTX
Laravel Beginners Tutorial 1
Vikas Chauhan
 
KEY
Php 101: PDO
Jeremy Kendall
 
PDF
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
Shengyou Fan
 
PPT
How PHP Works ?
Ravi Raj
 
PDF
LaravelConf Taiwan 2017 開幕
Shengyou Fan
 
PDF
Route 路由控制
Shengyou Fan
 
PHP 7 new engine
julien pauli
 
Being functional in PHP (PHPDay Italy 2016)
David de Boer
 
Internet of Things With PHP
Adam Englander
 
PHP Optimization
djesch
 
PHP WTF
markstory
 
PHP 7 performances from PHP 5
julien pauli
 
Php internal architecture
Elizabeth Smith
 
Laravel Beginners Tutorial 1
Vikas Chauhan
 
Php 101: PDO
Jeremy Kendall
 
[Community Open Camp] 給 PHP 開發者的 VS Code 指南
Shengyou Fan
 
How PHP Works ?
Ravi Raj
 
LaravelConf Taiwan 2017 開幕
Shengyou Fan
 
Route 路由控制
Shengyou Fan
 
Ad

Similar to PHP, Under The Hood - DPC (20)

PPTX
Php’s guts
Elizabeth Smith
 
PDF
Php engine
julien pauli
 
PDF
PHP Internals and Virtual Machine
julien pauli
 
PDF
Introduction to PHP (SDPHP)
Eric Johnson
 
PPTX
Php extensions
Elizabeth Smith
 
PDF
Php7 extensions workshop
julien pauli
 
PPT
Introduction to PHP - SDPHP
Eric Johnson
 
PPTX
Zend Framework Workshop
10n Software, LLC
 
PPTX
PHP ITCS 323
Sleepy Head
 
PDF
Phpをいじり倒す10の方法
Moriyoshi Koizumi
 
PPTX
Introduction to PHP.pptx
SherinRappai
 
PPT
Php
Ajay Kumar
 
PPT
Php classes in mumbai
Vibrant Technologies & Computers
 
PPTX
Php extensions
Elizabeth Smith
 
PPTX
Php Extensions for Dummies
Elizabeth Smith
 
PPTX
Php extensions
Elizabeth Smith
 
PDF
Php
Vineet Vats
 
PPT
Prersentation
Ashwin Deora
 
PPT
Synapseindia reviews on array php
saritasingh19866
 
PDF
Php extensions workshop
julien pauli
 
Php’s guts
Elizabeth Smith
 
Php engine
julien pauli
 
PHP Internals and Virtual Machine
julien pauli
 
Introduction to PHP (SDPHP)
Eric Johnson
 
Php extensions
Elizabeth Smith
 
Php7 extensions workshop
julien pauli
 
Introduction to PHP - SDPHP
Eric Johnson
 
Zend Framework Workshop
10n Software, LLC
 
PHP ITCS 323
Sleepy Head
 
Phpをいじり倒す10の方法
Moriyoshi Koizumi
 
Introduction to PHP.pptx
SherinRappai
 
Php classes in mumbai
Vibrant Technologies & Computers
 
Php extensions
Elizabeth Smith
 
Php Extensions for Dummies
Elizabeth Smith
 
Php extensions
Elizabeth Smith
 
Prersentation
Ashwin Deora
 
Synapseindia reviews on array php
saritasingh19866
 
Php extensions workshop
julien pauli
 

More from Anthony Ferrara (11)

PDF
Password Storage And Attacking In PHP - PHP Argentina
Anthony Ferrara
 
PDF
Beyond design patterns phpnw14
Anthony Ferrara
 
PDF
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Anthony Ferrara
 
PDF
Development By The Numbers - ConFoo Edition
Anthony Ferrara
 
PDF
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Anthony Ferrara
 
PDF
Don't be STUPID, Grasp SOLID - North East PHP
Anthony Ferrara
 
PDF
Development by the numbers
Anthony Ferrara
 
PDF
Don't Be Stupid, Grasp Solid - MidWestPHP
Anthony Ferrara
 
PDF
Cryptography For The Average Developer - Sunshine PHP
Anthony Ferrara
 
PDF
Password Storage and Attacking in PHP
Anthony Ferrara
 
PDF
Cryptography For The Average Developer
Anthony Ferrara
 
Password Storage And Attacking In PHP - PHP Argentina
Anthony Ferrara
 
Beyond design patterns phpnw14
Anthony Ferrara
 
Don't Be STUPID, Grasp SOLID - ConFoo Edition
Anthony Ferrara
 
Development By The Numbers - ConFoo Edition
Anthony Ferrara
 
Don't Be STUPID, Grasp SOLID - DrupalCon Prague
Anthony Ferrara
 
Don't be STUPID, Grasp SOLID - North East PHP
Anthony Ferrara
 
Development by the numbers
Anthony Ferrara
 
Don't Be Stupid, Grasp Solid - MidWestPHP
Anthony Ferrara
 
Cryptography For The Average Developer - Sunshine PHP
Anthony Ferrara
 
Password Storage and Attacking in PHP
Anthony Ferrara
 
Cryptography For The Average Developer
Anthony Ferrara
 

Recently uploaded (20)

PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 

PHP, Under The Hood - DPC