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 changed every second. When I hit a button that uses Javascript to ask PHP to get that value, does it display what the value currently is, or what it was at page load?
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 changed every second. When I hit a button that uses Javascript to ask PHP to get that value, does it display what the value currently is, or what it was at page load?
Share Improve this question edited Apr 24, 2014 at 1:29 evamvid asked Apr 24, 2014 at 1:18 evamvidevamvid 8616 gold badges18 silver badges41 bronze badges 3- 1 PHP do the work when you call it with the button. – Victor Martins Commented Apr 24, 2014 at 1:21
- 1 Well you can technically flush the output buffer and keep executing a script. But for what you want, research into ajax techniques. – Scuzzy Commented Apr 24, 2014 at 1:22
- Yes. See: https://stackoverflow./questions/4236040/example-of-how-to-use-fastcgi-finish-request – John Dorner Commented Jul 9, 2019 at 21:18
3 Answers
Reset to default 6I 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 eback) 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 pute that script according the way it understands it and sends only the puted result back to the server, as plain HTML. In this case the number 2
.
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. browser sends a request to the server of foo.
- 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, piles the query
- and sends the result as HTMl
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.
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.