# Behind the Scenes

> Section in “Introduction” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/introduction/behind-the-scenes/>

Many of the enhancements brought about by PHP 7 – from performance improvements to reduced memory footprint, from increased maintainability of PHP runtime’s C source code to new language features that make your daily work more convenient – are enabled by changes “behind the scenes” and “under the hood”. To better understand these, let us shine a light on how PHP compiles and executes code!

One of the key characteristics of a programming language is how programs written in it are executed. *Compiled languages* and *interpreted languages* are categories commonly used for this distinction. Most developers will immediately think of C, C++, C#, and Java as examples for compiled languages and Perl, PHP, Python, and Ruby as examples for interpreted languages.

A compiler is a piece of software that is used to translate source code from a higher level language such as C or Java to a lower level language such as native machine code or Java bytecode, respectively. Thinking in terms of *compiled* versus *interpreted*, however, is not very helpful when reasoning about programming languages because the runtime environment – the interpreter – of interpreted languages also contains a compiler.

Another characterization approach could be to talk about *ahead-of-time* compilation versus *on-demand* compilation. Programming languages such as C, C++, C#, and Java require an explicit compilation step before a program can be executed. The compiler has to be invoked, either on the command-line or from within an IDE, to compile the source code into a so-called binary.

In the case of C and C++ that binary usually contains native machine code while the compilers for C# and Java will generate binaries that contain *bytecode* for the respective *virtual machines* Common Language Runtime (CLR) and Java Virtual Machine (JVM). Bytecode is a compiled representation of a program that can be executed using a program called a virtual machine or software interpreter.

In contrast to compiled languages – languages that require an explicit, ahead-of-time compilation step – are interpreted languages. Perl, PHP, Python, and Ruby, for instance, do not need an explicit compilation step, but instead their interpreter implicitly compiles the source code when needed.

Early implementations of interpreted languages were compiled line-by-line. After a line of code was compiled it was executed immediately. If a line of code was to be executed multiple times, for instance when it was written in the body of a loop, then it needed to be recompiled each time. This is how PHP 3 worked, by the way. Since PHP 4, the PHP interpreter compiles PHP source code to an intermediate representation called PHP bytecode which is then executed.

## In this section

- [Compiler](https://php7explained.com/introduction/behind-the-scenes/compiler/index.html.md): The PHP 7 compiler works in three phases: lexical analysis with semi-reserved words, parsing into an abstract syntax tree, and bytecode generation.
- [Bytecode Execution](https://php7explained.com/introduction/behind-the-scenes/bytecode-execution/index.html.md): PHP 7 executor optimizations: zvals for simple values no longer need allocation, and hash tables were reengineered, including packed hash tables.
- [Bytecode Caching](https://php7explained.com/introduction/behind-the-scenes/bytecode-caching/index.html.md): How OpCache caches compiled bytecode, and how the file-based cache added in PHP 7 (opcache.file_cache) enables bytecode caching for the PHP CLI.
- [Bytecode Optimization](https://php7explained.com/introduction/behind-the-scenes/bytecode-optimization/index.html.md): How OpCache optimizes bytecode, controlled by opcache.optimization_level, using SSA form, type inference, and elimination of dead variables and code.
- [Debugger](https://php7explained.com/introduction/behind-the-scenes/debugger/index.html.md): The phpdbg interactive debugger, shipped since PHP 5.6, gains code coverage in PHP 7 and can collect coverage for PHPUnit without Xdebug or PCOV.

---

Previous: [Windows](https://php7explained.com/introduction/windows/index.html.md) | Next: [Compiler](https://php7explained.com/introduction/behind-the-scenes/compiler/index.html.md)
