[ACCEPTED]-PHP compile time vs run time. Understanding the difference-php

Accepted answer
Score: 31

PHP makes two passes (by default) anytime 17 it runs a file.

Pass #1 parses the file and 16 builds what is called operational(or machine) code. This 15 is the raw binary format your computer will 14 actually run and it is not human readable. In 13 other languages (like C++, etc) this is 12 called compiling. You can cache this step 11 using various systems like Opcache, which saves 10 you the overhead of compiling this every 9 time.

Syntax errors come from this portion 8 of the execution.

Pass #2 executes the operational 7 code from Pass #1. This is what is commonly 6 called "run time", because your 5 computer is actually executing the instructions.

Run-time 4 errors (like exhausting memory, abnormal 3 termination, etc) come from this level. These 2 are considerably less common than syntax 1 errors, however.

Score: 15

PHP files are run in two stages.

First, the 24 PHP files are parsed. At this point, the 23 data coming from the web browser (or from 22 any other source) is utterly irrelevant. All 21 this does is break the PHP file down into 20 its constituent parts and building the structure 19 of the code.

Then the code is executed with 18 the data you supply.

This separation makes 17 the code a very great deal faster. This 16 is especially true when you have opcode 15 caches like APC or OPcache, because the 14 first step can be skipped on subsequent 13 occasions because the structure of the code 12 is exactly the same.

The time when you will 11 encounter the difference is principally 10 with errors. For instance, this code will 9 cause an error at the compiling stage:

function class() {
    // some code

This 8 is not possible because class is a reserved word. PHP 7 can pick this up at the time the code is 6 compiled: it will always fail. It can never work.

This 5 code, however, could cause an error at runtime:

echo $_GET['nonExistingKey'];

Since 4 the key nonExistingKey doesn't exist, it can't be retrieved, so 3 it causes an error. However, PHP can't decide 2 this when the code is originally compiled, only 1 when it is run with the data you supply.

More Related questions