๐ What Happens When a PHP File is Called? (Under the Hood of PHP Execution)
Ever wondered what really happens when your browser requests a PHP file like hello.php? In this post, weโll dive deep into PHPโs execution lifecycle โ from a userโs request all the way to the response output. We'll explore how PHP parses your code, compiles it to opcodes, and executes it using the Zend Engine.
๐น 1. The Starting Point: A Simple PHP File
Letโs consider a very basic example:
<?php
$name = "Rahim";
echo "Hello, $name!";
You place this in your web server's root directory (like htdocs or public_html) and access it via:
http://localhost/hello.php
Now letโs trace what happens internally.
๐น 2. The HTTP Request Hits the Web Server
When you open that URL in your browser:- The browser sends an HTTP GET request for /hello.php.
- The web server (e.g., Apache or Nginx) receives the request.
- It checks the file extension, sees itโs a PHP file, and passes it to the PHP interpreter via:
- mod_php (Apache)
- PHP-FPM (FastCGI Process Manager โ commonly used)
Think of PHP-FPM as a middleman that manages PHP processes, waiting to run scripts when the web server asks.
๐น 3. Inside the PHP Engine
Now the PHP Engine (powered by the Zend Engine) begins its job. But it doesn't just read your code line-by-line โ it goes through 3 major stages:
โ
A. Lexical Analysis (Tokenizing)
PHP first breaks the code into tokens, which are small pieces of syntax.
$name = "Rahim";
Would be tokenized into:- T_VARIABLE โ $name
- T_EQUAL โ =
- T_CONSTANT_ENCAPSED_STRING โ "Rahim"
๐ง PHP has a built-in tokenizer (token_get_all()) that can show these tokens.
โ
B. Parsing into AST (Abstract Syntax Tree)
The tokens are converted into an Abstract Syntax Tree โ a structured tree that represents the logic of your code.
It now understands that:- A variable is being assigned a string
- A message is being printed
Parsing checks for syntax correctness at this stage.
โ
C. Compiling to Opcodes
The AST is compiled into opcodes โ a low-level set of instructions that the Zend Virtual Machine (VM) can execute.
For our script, PHP might generate simplified opcodes like:
1: ASSIGN $name "Rahim"
2: CONCAT "Hello, " $name
3: ECHO result
๐ก These arenโt visible by default, but you can see them using tools like vld or phpdbg.
๐น 4. Zend Engine Executes the Opcodes
Now the Zend VM executes these opcodes line-by-line:- Assigns "Rahim" to $name
- Concatenates "Hello, " with $name
- Outputs: Hello, Rahim!
All of this is executed in memory.
๐น 5. Output Buffering and Response Delivery
PHP doesnโt immediately send echo output to the browser.- It uses output buffering โ storing output in memory until the script finishes or flushes it.
- Once complete, it sends the full content to the web server.
- The web server wraps it in an HTTP response and returns it to the browser.
So your browser finally displays:
Hello, Rahim!
๐ Summary Flow (Visual)
User Request โ Web Server (Apache/Nginx)
โ
PHP-FPM / mod_php
โ
Zend Engine:
- Tokenize
- Parse (AST)
- Compile to Opcodes
- Execute Opcodes
โ
Output Buffer
โ
Web Server โ Browser
๐ง Bonus Concepts to Know
Zend Engine | The core runtime of PHP that parses, compiles, and executes
Opcodes | Low-level instructions generated from PHP code
AST | Tree representation of code logic
PHP-FPM | FastCGI manager that handles PHP execution
Output Buffering | Temporary storage for printed output
๐งช See It in Action
Want to see PHP under the hood? Try these:
โถ๏ธ Token View:
<?php
print_r(token_get_all('<?php echo "Hi"; ?>'));
โถ๏ธ View Opcodes:
Install vld extension:
php -dvld.active=1 hello.php
Or use https://3v4l.org to test code and view behavior across PHP versions.
๐ฏ Final Thoughts
PHP might look simple on the surface, but under the hood it has a powerful and structured execution process. Understanding how PHP tokenizes, parses, compiles, and executes code helps you write better, faster, and more efficient PHP applications โ and gives you a better appreciation of frameworks like Laravel that build on this foundation.
Comments
Leave a Comment
No comments yet. Be the first to comment!