PHP
Under the Hood
/haskellcamargo
marcelocamargo@linuxmail.org
About me
- PHP community contributor
- Creator of Quack programming language
- Free software translator
- Author of "Skype for Linux" 2015
- Author of "Gemidão do Zap" ✆
- Compilers developer
- Functional programming evangelist
- A cat lover!
YOU PROBABLY
CODE PHP EVERYDAY
But did you wonder how it works or still think it's dark magic?
THE INPUT PROGRAM
factorial(n: int): int
1 * 2 * 3 * 4 * 5
THE SERVER REQUEST
THE
LEXER
What is a lexer?
- Receives characters and outputs words
- Words are easier to analyse than characters!
Hello, how are you?
[HELLO][,][HOW][ARE][YOU][?]
And PHP also does that to your source!
It breaks everything in "tokens"
And it also exposes:
token_get_all(string): array
THE
PARSER
The parser
- Receives tokens, PHP <= 5.6: emits opcodes
- Receives tokens, PHP >= 7: generates AST
- Uses a formal "grammar"
- A grammar is a definition of rules of the language
Every implemented compiler has a parser
The parser does:
- Resolve precedence rules
The parser does:
- Resolve associativity rules
It detects syntax errors
THE SYMBOL TABLE
PHP uses hashtables to store variables and control scope
Variable | Value |
---|---|
$_GET | array(...) |
$_POST | array(...) |
$_GLOBALS | array(...) |
Global scope
Variable | Value |
---|---|
$n | 5 |
$i | 1 |
$result | 1 |
Factorial scope
THE VM AND OPCODES
The 💖 of PHP
PHP runs in a register based VM
- The Zend Virtual Machine
- It handles opcodes
- Something like a "machine independent" assembly
- It has a lot of instructions to make things easier!
Introducing opcodes
Opcodes are simple instructions that may take operands
Know the VM
and write faster code
DEBUG IT!
Variable | Value |
---|---|
IP | 0 |
Variable | Value |
---|---|
IP | 0 |
!0 | 5 |
Variable | Value |
---|---|
IP | 1 |
!0 | 5 |
!1 | 1 |
Variable | Value |
---|---|
IP | 2 |
!0 | 5 |
!1 | 1 |
!2 | 1 |
Variable | Value |
---|---|
IP | 3 |
!0 | 5 |
!1 | 1 |
!2 | 1 |
Variable | Value |
---|---|
IP | 7 |
!0 | 5 |
!1 | 1 |
!2 | 1 |
~7 | 1 |
1 <= 5
Variable | Value |
---|---|
IP | 8 |
!0 | 5 |
!1 | 1 |
!2 | 1 |
~7 | 1 |
If ~7 is not 0, go to instruction 4
Variable | Value |
---|---|
IP | 4 |
!0 | 5 |
!1 | 1 |
!2 | 1 |
~7 | 1 |
1 * 1
Variable | Value |
---|---|
IP | 5 |
!0 | 5 |
!1 | 1 |
!2 | 2 |
~7 | 1 |
~6 | 1 |
Variable | Value |
---|---|
IP | 6 |
!0 | 5 |
!1 | 1 |
!2 | 2 |
~7 | 1 |
2 <= 5
Variable | Value |
---|---|
IP | 7 |
!0 | 5 |
!1 | 1 |
!2 | 2 |
~7 | 1 |
Variable | Value |
---|---|
IP | 8 |
!0 | 5 |
!1 | 1 |
!2 | 2 |
~7 | 1 |
Variable | Value |
---|---|
IP | 4 |
!0 | 5 |
!1 | 2 |
!2 | 2 |
~7 | 1 |
1 * 2
Variable | Value |
---|---|
IP | 5 |
!0 | 5 |
!1 | 2 |
!2 | 3 |
~7 | 1 |
~6 | 2 |
Variable | Value |
---|---|
IP | 6 |
!0 | 5 |
!1 | 2 |
!2 | 3 |
~7 | 1 |
Variable | Value |
---|---|
IP | 7 |
!0 | 5 |
!1 | 2 |
!2 | 3 |
~7 | 1 |
3 <= 5
Variable | Value |
---|---|
IP | 4 |
!0 | 5 |
!1 | 6 |
!2 | 3 |
~7 | 1 |
2 * 3
Variable | Value |
---|---|
IP | 5 |
!0 | 5 |
!1 | 6 |
!2 | 4 |
~7 | 1 |
~6 | 3 |
Variable | Value |
---|---|
IP | 6 |
!0 | 5 |
!1 | 6 |
!2 | 4 |
~7 | 1 |
Variable | Value |
---|---|
IP | 7 |
!0 | 5 |
!1 | 6 |
!2 | 4 |
~7 | 1 |
4 <= 5
Variable | Value |
---|---|
IP | 8 |
!0 | 5 |
!1 | 6 |
!2 | 4 |
~7 | 1 |
Variable | Value |
---|---|
IP | 4 |
!0 | 5 |
!1 | 24 |
!2 | 4 |
~7 | 1 |
6 * 4
Variable | Value |
---|---|
IP | 5 |
!0 | 5 |
!1 | 24 |
!2 | 5 |
~7 | 1 |
~6 | 4 |
Variable | Value |
---|---|
IP | 6 |
!0 | 5 |
!1 | 24 |
!2 | 5 |
~7 | 1 |
Variable | Value |
---|---|
IP | 7 |
!0 | 5 |
!1 | 24 |
!2 | 5 |
~7 | 1 |
5 <= 5
Variable | Value |
---|---|
IP | 8 |
!0 | 5 |
!1 | 24 |
!2 | 5 |
~7 | 1 |
Variable | Value |
---|---|
IP | 4 |
!0 | 5 |
!1 | 120 |
!2 | 5 |
~7 | 1 |
24 * 5
Variable | Value |
---|---|
IP | 5 |
!0 | 5 |
!1 | 120 |
!2 | 6 |
~7 | 1 |
~6 | 5 |
Variable | Value |
---|---|
IP | 6 |
!0 | 5 |
!1 | 120 |
!2 | 6 |
~7 | 1 |
Variable | Value |
---|---|
IP | 7 |
!0 | 5 |
!1 | 120 |
!2 | 6 |
~7 | 0 |
6 <= 5
Variable | Value |
---|---|
IP | 8 |
!0 | 5 |
!1 | 120 |
!2 | 6 |
~7 | 0 |
Variable | Value |
---|---|
IP | 9 |
!0 | 5 |
!1 | 120 |
!2 | 6 |
~7 | 0 |
120
Do you know opcache?
Resources to study
Resources to study
- PHP Internals book
- Compilers: principles, techniques and tools
- PHP: Zend Engine 2 Opcodes
- PHP: PHP at the Core: A Hacker's Guide
- Kaleidoscope: Implementing a Language with LLVM
Ok, thanks, bye!
😂😂👌👌👌🔝🔝🔝
PHP Under the Hood
By Marcelo Camargo
PHP Under the Hood
PHP Under the Hood
- 3,856