21

PHP is an interpreted language, not compiled. Yet I have come across a book that mentions stuff happening in PHP at compile time, and the PHP manual states that declaring a const happens at compile-time. How is the term compile-time used in relation to PHP since PHP isn't compiled?

If it is just meant as "when the script is read and translated into the interpreters subroutines", then what is the difference between the terms compile-time and run-time?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
TigerBear
  • 2,479
  • 1
  • 21
  • 24
  • 1
    @Dagon Easy there. The results of said research would lead the OP right to [this](http://stackoverflow.com/questions/1514676), indicating the contrary. The term "compile" has some subtleties here. – Jonathon Reinhart May 20 '14 at 00:40
  • 3
    @Dagon Thats pretty rude for someone who is wrong. The interpreter is compiled, but the php script isn't. See more here: http://stackoverflow.com/questions/1514676/is-php-compiled-or-interpreted – TigerBear May 20 '14 at 00:41
  • @Dagon It's not so simple as that. It is possible to compile PHP in the traditional sense, though that is by no means out-of-the-box functionality. There are steps that take place in interpretation that could be considered compilation. In fact, interpretation itself is compilation in a sense. However, the distinction between compilation and interpretation is made because there are significant differences. – Cully May 20 '14 at 00:42
  • @punchingInAPepper In that context, "compile time" could be used synonymously with "interpretation time". – Cully May 20 '14 at 00:43
  • 1
    well if your all going to pick on me i'm going to lunch. –  May 20 '14 at 00:43
  • @Dagon Trust me, I'm not afraid of closing "stupid n00b" questions. But this one is quite good, and not a duplicate (at least not of the linked question). – Jonathon Reinhart May 20 '14 at 00:44
  • 2
    "at compile-time" == "during translation of the script to opcode by the Zend engine, prior to its execution." – lafor May 20 '14 at 00:46
  • @lafor You should write an answer explaining this. – Jonathon Reinhart May 20 '14 at 00:49
  • @punchingInAPepper The top answer on that question is simply wrong (or at least incomplete and misleading). This answer in particular explains it well: http://stackoverflow.com/a/1514737/19405 – nobody May 20 '14 at 01:08
  • 3
    I commend @JonathonReinhart's commentary on this question. It's a sensible and clear question that is not answered by the "duplicate" the closers have pointed at. I have voted to reopen it and ask others to do the same. Meanwhile, some reasonable-looking answers can be found at http://stackoverflow.com/questions/23959337/php-compile-time-vs-run-time-understanding-the-difference. – Mark Amery Sep 29 '14 at 22:47

1 Answers1

9

PHP source code goes through a step where it is compiled into PHP Opcode. This idea has been implemented in a variety of platforms, most notably with Java. Theoretically, by having a separate "virtual machine" runtime to run the Opcodes, the language designers are able to separate the language from portability issues.

You can find a list of these Opcodes in the manual

In a typical PHP environment with no opcode caching, the compilation step and the "run-time" step are indistinguishable, however, when you introduce an "accelerator/opscode cache" like APC or the Zend Platform product, you can see that these are separate steps in the process.

Once a script has been compiled into PHP Opscodes, it can be run from cache without having to be re-compiled from source, which is where these accelerators can greatly improve performance.

If you focus in on the "runtime" aspect of PHP you see the "Interpreted" nature of PHP, as it requires a runtime environment, when compared to a compiled/linked language like c/c++ that runs as a native operating system program.

In the case of PHP, the php program is the native operating system program (or native as a module of a native OS web server).

Not unlike the way Java runs inside of the "Java Virtual Machine (JVM)" PHP's scripts are running inside PHP and thus don't contain the specifics of how the operations are going to be natively performed by the OS.

gview
  • 14,876
  • 3
  • 46
  • 51