• Home
  • blog
  • How PHP Executes – from Source Code to Render

How PHP Executes – from Source Code to Render

Inspired by a recent article on how Ruby code executes, this article covers the execution process for PHP code.

Flowchart vector image


There’s a lot going on under the hood when we execute a piece of PHP code. Broadly speaking, the PHP interpreter goes through four stages when executing code:

  1. Lexing
  2. Parsing
  3. Compilation
  4. Interpretation

This article will skim through these stages and show how we can view the output from each stage to really see what is going on. Note that while some of the extensions used should already be a part of your PHP installation (such as tokenizer and OPcache), others will need to be manually installed and enabled (such as php-ast and VLD).

Stage 1 – Lexing

Lexing (or tokenizing) is the process of turning a string (PHP source code, in this case) into a sequence of tokens. A token is simply a named identifier for the value it has matched. PHP uses re2c to generate its lexer from the zend_language_scanner.l definition file.

We can see the output of the lexing stage via the tokenizer extension:

Continue reading %How PHP Executes – from Source Code to Render%