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?
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?
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.
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.