Understanding PHP: From Code to Execution
What is PHP?
PHP (recursive acronym for PHP: Hypertext Preprocessor
) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.
What distinguishes PHP from something like client-side JavaScript is that the code is executed on the server, generating HTML which is then sent to the client.
Lets See how PHP Executes internally?
Here's a story that personifies different parts of the PHP ecosystem. Lets dive into the digital world of Web City, where lived a group of friends known as the PHP Pals. They worked together to process web requests and create dynamic web pages.
Our story begins with Reggie the Request, a curious little packet of data who arrived at the gates of Web City. The gatekeeper, Sammy the Server, greeted Reggie warmly.
"Welcome, Reggie! I see you're looking for a PHP page. Let me introduce you to the PHP Pals who'll help you out," said Sammy.
Sammy called over Tofu the Tokenizer, the first of the PHP Pals. Tofu was known for his keen eye and ability to break things down.
<?php
echo "Hello, World!";
?>
"Hi there!" chirped Tofu. "My job is to look at the PHP code and break it into small, meaningful chunks called tokens. It's like taking a sentence and identifying each word and punctuation mark."
//Tokens
[T_OPEN_TAG] <?php
[T_ECHO] echo
[T_WHITESPACE]
[T_CONSTANT_ENCAPSED_STRING] "Hello, World!"
[T_SEMICOLON] ;
[T_CLOSE_TAG] ?>
Tofu got to work, carefully examining the PHP code and breaking it into tokens. Once done, he passed the tokens to his friend, Pete the Parser.
Pete adjusted his glasses and said, "Thanks, Tofu! I'll take it from here. My job is to take these tokens and figure out how they all fit together. I create what we call an Abstract Syntax Tree, or AST for short."
Pete worked his magic, arranging the tokens into a tree-like structure that represented the code's structure and meaning. He then handed this AST to Olga the Optimizer.
AST_STMT_LIST //AST
└─ AST_ECHO
└─ AST_STRING ("Hello, World!")
Olga flexed her muscles and grinned. "Time to make this code lean and mean! I'll look at the AST and see if there are any ways to make it run faster or use less memory."
After Olga finished her optimizations, she passed the improved AST to Cody the Compiler.
Cody, always efficient, explained, "I transform this AST into something called opcode. It's like translating the recipe into a set of precise instructions that can be followed step by step."
//opcode
Line Opcode Operands
--------------------------------------------
1 ECHO 'Hello, World!'
2 RETURN 1
Finally, the opcode reached Ellie the Executor.
"This is where the magic happens!" Ellie exclaimed. "I take this opcode and actually run it, following each instruction to create the final output."
Ellie worked swiftly, executing the opcode and generating the response. She handed the response back to Sammy the Server, who then sent it off with Reggie the Request back to the Client who made the original request.
"Another job well done, PHP Pals!" cheered Sammy, as they all high-fived each other.
Subscribe to my newsletter
Read articles from anmol gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by