Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP compile time vs run time. Understanding the difference [duplicate]

Tags:

php

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?

like image 391
An_roid Avatar asked May 30 '14 16:05

An_roid


People also ask

What is the difference between compile time and run time?

Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.

Is PHP runtime or compile time?

Basically, PHP is interpreted but PHP is compiled down to an intermediate bytecode that is then interpreted by the runtime Zend engine.

What is the major difference between compile time and run time error?

A compile-time error generally refers to the errors that correspond to the semantics or syntax. A runtime error refers to the error that we encounter during the code execution during runtime. We can easily fix a compile-time error during the development of code. A compiler cannot identify a runtime error.

Which is faster runtime or compile time?

Basically if your compiler can work out what you mean or what a value is "at compile time" it can hardcode this into the runtime code. Obviously if your runtime code has to do a calculation every time it will run slower, so if you can determine something at compile time it is much better.


2 Answers

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.

like image 117
Machavity Avatar answered Oct 22 '22 11:10

Machavity


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.

like image 15
lonesomeday Avatar answered Oct 22 '22 11:10

lonesomeday