how PHP works
-
Can someone help me understand whether PHP is compiled or interpreted? I googled it and was left very confused. It sounds like PHP is compiled to some type of bytecode. But, I have never seen another file created (an executable). I can't imagine PHP would re-compile each script.php every time it is used, but I don't see any .exe files getting created. Anyone know how PHP really works? It seems like almost no one knows. Thanks.
-
Can someone help me understand whether PHP is compiled or interpreted? I googled it and was left very confused. It sounds like PHP is compiled to some type of bytecode. But, I have never seen another file created (an executable). I can't imagine PHP would re-compile each script.php every time it is used, but I don't see any .exe files getting created. Anyone know how PHP really works? It seems like almost no one knows. Thanks.
-
I don't know what you Googled, but the links in the following search make it clear that PHP is interpreted as it runs: is php compiled - Google Search[^].
There is not a clear, absolute distinction. Old style interpreters would interpret the statements of a loop from source code on every iteration of the loop, and similar with other constructs. To speed up execution, interpreters began (at least 25 years ago, maybe earlier) when analyzing a statement, to leave the analysis in a memory cache. So for a loop, the analysis was done the first time through. Following iterations skipped the analysis step, and rather picked up the analysis from the cache. As this became more common, the analysis results became more formalized into some variant of P-code, suitable for direct interpretation. When done as a separate step, for an entire program or program module (e.g. the classic Pascal compiler from ETH Zürich), it is always called a compiler. So when the php runtime system does the same thing for a loop, you might say that it is a compiler, compiling that loop. Another change over time: The first interpreters to save analysis results for later use did it line by line, or statement by statement. More recent interpreters compile larger units, e.g. a complete method, in order to apply optimizations such as moving invariants out of loops, calculating common expressions once only etc. If the generated code follows a well defined grammar, the runtime compiler may save it to a file or cache. Compare it to dotNet: The IL code(*) of an assembly is compiled to binary machine code by the "jitter" (Just In Time compiler) first time it is run. The jitter also saves the binary code in a (persistent) disk cache that is usually not seen by neither programmer nor user; it is in a file space managed by the jitter alone. Next time the same assembly is run, the jitter first looks in its cache: If an already compiled version is found there, it is loaded, and the JIT compiling is bypassed. A similar (persistent) caching (of P-code) might be employed by an interpreter. It should not affect the source language - the same source may be interpreted on one machine, compiled to P-code on the fly on every execution on another machine, while a third machine may have an interpreter looking in its cache for an already compiled variant. This may be applied to a lot of different languages: You could make an interpreter to P-code on the fly, for subsequent immediate interpretation by an interpreter. Usually, you think of Java as a compiled language, but if you integrate JVM with the compiler, they might appear externally just as 'interpreted' as, say, PHP. (*) dotNet IL code and P-
-
There is not a clear, absolute distinction. Old style interpreters would interpret the statements of a loop from source code on every iteration of the loop, and similar with other constructs. To speed up execution, interpreters began (at least 25 years ago, maybe earlier) when analyzing a statement, to leave the analysis in a memory cache. So for a loop, the analysis was done the first time through. Following iterations skipped the analysis step, and rather picked up the analysis from the cache. As this became more common, the analysis results became more formalized into some variant of P-code, suitable for direct interpretation. When done as a separate step, for an entire program or program module (e.g. the classic Pascal compiler from ETH Zürich), it is always called a compiler. So when the php runtime system does the same thing for a loop, you might say that it is a compiler, compiling that loop. Another change over time: The first interpreters to save analysis results for later use did it line by line, or statement by statement. More recent interpreters compile larger units, e.g. a complete method, in order to apply optimizations such as moving invariants out of loops, calculating common expressions once only etc. If the generated code follows a well defined grammar, the runtime compiler may save it to a file or cache. Compare it to dotNet: The IL code(*) of an assembly is compiled to binary machine code by the "jitter" (Just In Time compiler) first time it is run. The jitter also saves the binary code in a (persistent) disk cache that is usually not seen by neither programmer nor user; it is in a file space managed by the jitter alone. Next time the same assembly is run, the jitter first looks in its cache: If an already compiled version is found there, it is loaded, and the JIT compiling is bypassed. A similar (persistent) caching (of P-code) might be employed by an interpreter. It should not affect the source language - the same source may be interpreted on one machine, compiled to P-code on the fly on every execution on another machine, while a third machine may have an interpreter looking in its cache for an already compiled variant. This may be applied to a lot of different languages: You could make an interpreter to P-code on the fly, for subsequent immediate interpretation by an interpreter. Usually, you think of Java as a compiled language, but if you integrate JVM with the compiler, they might appear externally just as 'interpreted' as, say, PHP. (*) dotNet IL code and P-
-
Because a public post is intended for the entire reading audience, not just for you alone. I wrote my post to expand on your WDYJFGI style reply; that is why it came as a follow up to yours. I had a coworker who was a real nuisance in informal conversations: All the time he interrupted "You have told that earlier!", and I had to reply: "Yes, to you, but this was John asking, and he hasn't heard it yet!" This guy never learned; it happened again and again. For some reason, your reply/question made me think of this fellow.
Religious freedom is the freedom to say that two plus two make five.
-
Because a public post is intended for the entire reading audience, not just for you alone. I wrote my post to expand on your WDYJFGI style reply; that is why it came as a follow up to yours. I had a coworker who was a real nuisance in informal conversations: All the time he interrupted "You have told that earlier!", and I had to reply: "Yes, to you, but this was John asking, and he hasn't heard it yet!" This guy never learned; it happened again and again. For some reason, your reply/question made me think of this fellow.
Religious freedom is the freedom to say that two plus two make five.
-
Can someone help me understand whether PHP is compiled or interpreted? I googled it and was left very confused. It sounds like PHP is compiled to some type of bytecode. But, I have never seen another file created (an executable). I can't imagine PHP would re-compile each script.php every time it is used, but I don't see any .exe files getting created. Anyone know how PHP really works? It seems like almost no one knows. Thanks.
I'm gonna delete this message because I have a stalker here that I don't want stalking me. But, I can help with one part of your question at least...
mike7411 wrote:
It sounds like PHP is compiled to some type of bytecode. But, I have never seen another file created (an executable).
PHP does in fact compile to bytecode before it's executed but like with Node it's done in memory, just in time (JIT compiling). So, you won't see it saved to a disk with one exception. There are [PHP Accelerators](https://wp-rocket.me/blog/best-php-accelerators/) that will effectively do the same thing but serialize the bytecode so it's cached at build time rather than run time or in a shared memory space, etc. However, these are _usually_ third party addons. I say usually, because Zend (company behind PHP) has had one for a while, but they decided to integrate into PHP itself. You can still find third party ones, but no need to. PHP ships with [PHP: OPcache](https://www.php.net/manual/en/book.opcache.php). Which basically uses shared memory to store the bytecode that's only JITed once and used to execute as many times as you want. Effectively bypassing that stage with subsequent executions. I'm reasonably sure you can configure it to serialize the cache as well, but you may want to look into that. Just know, out of the box, with vanilla PHP, you won't see the bytecode unless you start inspecting memory.
Jeremy Falcon
-
Can someone help me understand whether PHP is compiled or interpreted? I googled it and was left very confused. It sounds like PHP is compiled to some type of bytecode. But, I have never seen another file created (an executable). I can't imagine PHP would re-compile each script.php every time it is used, but I don't see any .exe files getting created. Anyone know how PHP really works? It seems like almost no one knows. Thanks.
PHP is a scripting language, which basically means there is an "exe" somewhere reading the php script every time there is a request. For php that will be Zend Engine.