Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. how PHP works

how PHP works

Scheduled Pinned Locked Moved Web Development
phptoolshelpquestion
8 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    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.

    L J U 3 Replies Last reply
    0
    • M mike7411

      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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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[^].

      T 1 Reply Last reply
      0
      • L Lost User

        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[^].

        T Offline
        T Offline
        trønderen
        wrote on last edited by
        #3

        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-

        L 1 Reply Last reply
        0
        • T trønderen

          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-

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sorry, why atre you telling me this?

          T 1 Reply Last reply
          0
          • L Lost User

            Sorry, why atre you telling me this?

            T Offline
            T Offline
            trønderen
            wrote on last edited by
            #5

            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.

            L 1 Reply Last reply
            0
            • T trønderen

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Well maybe so, but it would have been better posted in reply to the question, as the OP is the one asking for the information. I suspect that very few people will actually read your response as it does not appear to be an answer to the question.

              1 Reply Last reply
              0
              • M mike7411

                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.

                J Offline
                J Offline
                Jeremy Falcon
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • M mike7411

                  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.

                  U Offline
                  U Offline
                  User 10216595
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups