15

I hear those two terms often and keep thinking

What is the difference between compile time vs run time in PHP?

I have tried reading some articles, but it didn't help.

Does anyone know of a simple explanation? How are they different from each other?

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
An_roid
  • 159
  • 1
  • 5
  • 4
    compile-time is when the script file is loaded into memory and tokenized (if you're using an opcode cache, this is a one-time exercise when the script is first requested); run time is when that code actually executes – Mark Baker May 30 '14 at 16:33
  • 2
    PHP is an interpreted language so it works very differently from say C++ – mclaassen May 30 '14 at 16:33
  • 3
    As They other two replies have already stated, PHP is an interperted language. What this means in lamens terms is that you can load your raw `.php` files to your PHP engine (webserver / whatever) and they will execute. Other languages, like Java, C#, and C++, are compiled. What this means is that the raw files are actually not loaded to your application, rather a compiler reads those files and builds an executeable. – mituw16 May 30 '14 at 16:35

2 Answers2

35

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

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

Syntax errors come from this portion of the execution.

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

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

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 2
    So how can certain codes run at compile time and other during run time? According to your description, it seems all code throw an error or not during run time – An_roid May 30 '14 at 16:53
  • 2
    Depends on the error. Syntax errors will be caught in compilation. Run time errors (like your process exhausting the allocated memory) would be thrown from run time. – Machavity May 30 '14 at 16:55
  • 2
    I think this would help: http://bayanbox.ir/view/8066466214302126886/php-internals.jpg – Yousha Aleayoub Mar 05 '19 at 11:47
16

PHP files are run in two stages.

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

Then the code is executed with the data you supply.

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

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

function class() {
    // some code
}

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

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

echo $_GET['nonExistingKey'];

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

lonesomeday
  • 233,373
  • 50
  • 316
  • 318