Can PHP run after the page is loaded?

后端 未结 3 1079
说谎
说谎 2021-01-14 09:38

So PHP is executed server-side. But is it possible for PHP to be run after the page is loaded?

To illustrate, if I had a value (stored in a file, lets say) that chan

相关标签:
3条回答
  • 2021-01-14 10:02

    PHP is server-side script, before return page content to client side like browser, it should parse all the PHP logic into HTML logic, so it should display the value which was at page load. And if your PHP logic consumes long time to execute, it will delay the content display at client side.

    0 讨论(0)
  • 2021-01-14 10:08

    I think you need to get one of those diagrams that show how basic HTTP and the web server works. It will make more sense to you, than explained in plain words here.

    In the simplest possible case, the result of you typing some address and getting a web page with its contents can be summed up, due to a result of process in request/response relationship between your browser and a web server located somewhere in the world.

    Plain HTML

    In a less simpler way, think of it like this. basically, if a page is during a refresh phase, (meaning you clicked something and are waiting for a data to comeback) then, that means it is getting/loading the response from the web server. If the web server does not have PHP installed as a module, then the only thing it is waiting/loading (in many cases) is plain HTML content.

    With PHP

    On the other hand, if we assume you have a file called index.php in your webserver, and have PHP is installed, in this case the web server will send everything that appears in-between <?php ?> to the PHP interpreter, first, then wait for it until PHP does its magic and send back to the server only the result.

    <?php
     echo 1+1; 
    ?>
    

    So, in the above case, the webserver (ex: Apache, Nginx) does not care what is inside the opening and closing tags, and sends the entire code to the PHP interpreter, and PHP would compute that script according the way it understands it and sends only the computed result back to the server, as plain HTML. In this case the number 2.

    enter image description here

    The role of AJAX.

    AJAX (Asynchronous JavaScript and XML) is a technique used Javascript, to help you send requests and receive the response without having to load the page. This is usually done by using the browsers XHR object. So, there is no mystery in this whole shebang.

    The above can be summed up simply in the following steps.

    • Enter foo.com browser sends a request to the server of foo.com
    • server/browser exchange messages server allows browser to aquire
    • information server sends index.php back to browser if <?php tag
    • is found in the script, server sends all the codes inclosed in those
    • tags to the PHP interpreter The PHP interpreter, compiles the query
    • and sends the result as HTMl
    0 讨论(0)
  • 2021-01-14 10:22

    Your example case is really difficult to explain, without getting technical, or using a lot of chained logic...

    ...however, I'll try to keep this a little more simple:

    The overwhelmingly vast majority of the time, PHP will run only when something connects to the server, and will stop running as soon as it's done running the script that was accessed.

    That "something" might be a page-load (//mysite/index.php), or it might be issuing an XMLHttpRequest ("AJAX") to ask the server for data (//mysite/articles.json).

    Not all languages work this way.

    0 讨论(0)
提交回复
热议问题