Php 简明教程
PHP - Superglobals
PHP 解析器会用很多预定义变量及其全局命名空间填充当前脚本。预定义变量称为 “ PHP superglobals ”。
The PHP parser populates the current script with a number of predefined variables in its global namespace. The predefined variables are known as "PHP superglobals".
-
Any user defined variable declared outside of any function, method, or class also is a global variable. However, to access it, you need to use the global keyword.
-
In contrast, superglobals are always available anywhere in the PHP script, without mentioning them with the global keyword.
PHP 中的大多数超级全局变量都是关联数组,并且 Web 服务器会填充它们。因此,如果在命令行环境中运行脚本,则某些超级全局变量可能为空。
Most of the superglobals in PHP are associative arrays, and the web server populates them. Hence, if a script is run in the command-line environment, some of the superglobals may be empty.
PHP 中的超级全局变量列表包括以下内容:
The list of superglobal variables in PHP includes the following −
-
$GLOBALS
-
$_SERVER
-
$_GET
-
$_POST
-
$_FILES
-
$_COOKIE
-
$_SESSION
-
$_REQUEST
-
$_ENV
在本章中,我们将对 PHP 中的这些超级全局变量进行简要介绍。在后续章节中,我们将详细讨论这些超级全局变量。
In this chapter, we will have a brief introduction to these superglobal variables in PHP. In the subsequent chapters, we will discuss these superglobal variables in detail.
$GLOBALS
它是对所有全局定义变量的引用的关联数组。变量名称构成键,其内容是关联数组的值。
It is an associative array of references to all globally defined variables. Names of variables form keys and their contents are values of associative array.
$_SERVER
所有与服务器和执行环境相关的信息都包含在该关联数组中。
All the server and execution environment related information is available in this associative array.
5.4.0 之前的 PHP 版本包含以 $HTTP_SERVER_VARS 形式存在相同信息的变量,但现在已删除。
PHP versions prior to 5.4.0 contained $HTTP_SERVER_VARS contained the same information but has now been removed.
$_GET
它是通过附加至 HTTP 请求的 URL 的查询字符串传给当前脚本的变量的关联数组。请注意,除了 GET 请求之外,还通过包含查询字符串的所有请求填充该数组。
It is an associative array of variables passed to the current script via query string appended to URL of HTTP request. Note that the array is populated by all requests with a query string in addition to GET requests.
查询字符串是所有变量及其值按“var=val”形式形成的列表,并以“&”符号连接起来。
A query string is a list of all variables and their values in the form var=val and concatenated by the "&" symbol.
查询字符串本身在问号“?”之后附加至 PHP 脚本名称。例如 http://localhost/hello.php?first_name=Amar&last_name=Sharma 。
The query string itself is appended to the name of PHP script after the "?" symbol. For example, http://localhost/hello.php?first_name=Amar&last_name=Sharma.
$_POST
它是由 HTTP POST 方法通过查询字符串传给 URL 的键-值对的关联数组,在请求中使用 URLEncoded 或 multipart/form-data 内容类型。
It is an associative array of key-value pairs passed to a URL by HTTP POST method that uses URLEncoded or multipart/form-data content-type in request.
$HTTP_POST_VARS 还包含与 $_POST 相同的信息,但不是超级全局变量,现已不赞成使用。向服务器发送具有 POST 请求的数据的最简单方法是将 HTML 表单的方法属性指定为 POST。
$HTTP_POST_VARS also contains the same information as $_POST, but is not a superglobal, and now been deprecated. The easiest way to send data to a server with POST request is specifying the method attribute of HTML form as POST.
$_FILES
变量 $_FILES 是一个关联数组,其中包含通过 HTTP POST 方法上传的项。当一个 HTML 表单包含一个文件类型的输入元素,它的 enctype 属性设置为 multipart/form-data,并且方法属性设置为 HTTP POST 方法时,就会上传一个文件。
The variable $_FILES is an associative array containing items uploaded via HTTP POST method. A file is uploaded when a HTML form contains an input element with file type, its enctype attribute set to multipart/form-data, and the method attribute set to HTTP POST method.
$_COOKIE
Cookie 是服务器存储在客户端计算机上的文本文件,并且它们被保留以用于跟踪目的。
Cookies are text files stored by a server on the client computer and they are kept of use tracking purpose.
超级全局变量 $_COOKIE 将通过 HTTP 请求传递给当前 PHP 脚本的变量以 cookie 的形式存储起来。
The superglobal $_COOKIE stores variables passed to the current PHP script along with the HTTP request in the form of cookies.
$_SESSION
HTTP 会话是用户与服务器建立连接的时间持续期,以及连接被终止的时间之间的时间段。在此间隔期间,某些数据是以会话变量的形式永久存在于页面中。
An HTTP session is the time duration between the time a user establishes connection with a server and the time the connection is terminated. During this interval, some data is persistently available across pages in the form of session variables.
$_SESSION 超全局变量是会话变量的关联数组,当前脚本可以使用。
The $_SESSION superglobal is an associative array of session variables available to the current script.
$_REQUEST
$_REQUEST 是一个关联数组,它包含 $_GET、$_POST 和 $_COOKIE 变量的内容。
$_REQUEST is an associative array which is a collection of contents of $_GET, $_POST and $_COOKIE variables.
这些变量的顺序由 "php.ini" 文件中 requests_order 和 varables_order 设置的值决定。
The order of these variables is decided by the values of requests_order and varables_order settings in the "php.ini" file.