页面加载后可以运行PHP吗? [英] Can PHP run after the page is loaded?

查看:60
本文介绍了页面加载后可以运行PHP吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,PHP是在服务器端执行的.但是可以在页面加载后运行PHP吗?

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

举例说明,如果我有一个每秒更改的值(假设存储在文件中).当我按下使用Javascript要求PHP获取该值的按钮时,它是否显示当前值或页面加载时的值?

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?

推荐答案

我认为您需要获得一张显示基本HTTP和Web服务器如何工作的图.对您来说,这比这里简单的解释要有意义.

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.

在最简单的情况下,由于浏览器之间的 request/response 关系处理过程的结果,可以总结出输入地址并获取包含其内容的网页的结果和位于世界某处的Web服务器.

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.

以一种不太简单的方式,像这样思考.基本上,如果页面处于刷新阶段(意味着您单击了某些内容并正在等待数据恢复),则意味着该页面正在从Web服务器获取/加载响应.如果Web服务器未将PHP作为模块安装,那么它在等待/加载(在许多情况下)的唯一目的就是纯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.

另一方面,如果我们假设您的网络服务器中有一个名为 index.php 的文件,并且已安装PHP,则在这种情况下,网络服务器将发送介于两者之间的所有内容<?php?> 传递给PHP解释器,然后等待它直到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; 
?>

因此,在上述情况下,网络服务器(例如:Apache,Nginx)不在乎开始标记和结束标记中的内容,而是将整个代码发送给PHP解释器,PHP会根据以下方式计算该脚本它理解它,并且仅将计算结果作为纯HTML发送回服务器.在这种情况下,数字为 2 .

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.

AJAX(异步JavaScript和XML)是一种使用Javascript的技术,可帮助您发送请求和接收响应,而不必加载页面.通常,这是通过使用浏览器的XHR对象来完成的.因此,在整个shebang中都没有奥秘.

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.

  • 输入foo.com浏览器将请求发送到foo.com的服务器
  • 服务器/浏览器交换消息服务器允许浏览器获取
  • 如果<?php 标记,
  • 信息服务器将 index.php 发送回浏览器在脚本中找到
  • ,服务器将发送其中包含的所有代码
  • 标记到PHP解释器PHP解释器编译查询
  • 并将结果发送为HTMl
  • 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

这篇关于页面加载后可以运行PHP吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆