Superglobal variables, introduced in PHP 4.1.0, are built-in variables that are always available in all scopes.
PHP Global Variables - Superglobal Variables
Many of the predefined variables in PHP are "superglobal", meaning they are available throughout the entire scope of a script. They can be accessed within a function or method without executing global $variable;.
These superglobal variables are:
<code><span>$GLOBALS</span><span>$_SERVER</span><span>$_REQUEST</span><span>$_POST</span><span>$_GET</span><span>$_FILES</span><span>$_ENV</span><span>$_COOKIE</span><span>$_SESSION</span></code>
$GLOBALS — References all variables available in the global scope
$GLOBALS This type of global variable is used to access global variables anywhere in a PHP script (from a function or method Both are acceptable).
PHP stores all global variables in an array called $GLOBALS[index]. The name of the variable is the key of the array.
The following example shows how to use the super global variable $GLOBALS:
Example
<code><span><span><?php</span><span>$x</span> = <span>75</span>; <span>$y</span> = <span>25</span>; <span><span>function</span><span>addition</span><span>()</span> {</span><span>$GLOBALS</span>[<span>'z'</span>] = <span>$GLOBALS</span>[<span>'x'</span>] + <span>$GLOBALS</span>[<span>'y'</span>]; } addition(); <span>echo</span><span>$z</span>; <span>?></span></span></code>
PHP $_SERVER
$_SERVER This superglobal variable holds information about headers, paths and script locations.
The following example shows how to use certain elements in $_SERVER:
Example
<code><span><span><span><?php</span><span>echo</span><span>$_SERVER</span>[<span>'PHP_SELF'</span>]; <span>echo</span><span>"<br>"</span>; <span>echo</span><span>$_SERVER</span>[<span>'SERVER_NAME'</span>]; <span>echo</span><span>"<br>"</span>; <span>echo</span><span>$_SERVER</span>[<span>'HTTP_HOST'</span>]; <span>echo</span><span>"<br>"</span>; <span>echo</span><span>$_SERVER</span>[<span>'HTTP_REFERER'</span>]; <span>echo</span><span>"<br>"</span>; <span>echo</span><span>$_SERVER</span>[<span>'HTTP_USER_AGENT'</span>]; <span>echo</span><span>"<br>"</span>; <span>echo</span><span>$_SERVER</span>[<span>'SCRIPT_NAME'</span>]; <span>?></span></span></span></code>
The following table lists the most important elements you can access in $_SERVER:
PHP $_REQUEST
PHP $_REQUEST is used to collect data for HTML form submissions.
<code>下面的例子展示了一个包含输入字段及提交按钮的表单。当用户通过点击提交按钮来提交表单数据时, 表单数据将发送到 <form> 标签的 action 属性中指定的脚本文件。在这个例子中,我们指定文件本身来处理表单数据。如果您需要使用其他的 PHP 文件来处理表单数据,请修改为您选择的文件名即可。然后,我们可以使用超级全局变量 <span>$_REQUEST</span> 来收集 input 字段的值:</code>
Example
<code><span><<span>html</span>></span><span><<span>body</span>></span><span><<span>form</span><span>method</span>=<span>"post"</span><span>action</span>=<span>"<?php echo $_SERVER['PHP_SELF'];?>"</span>> Name: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"fname"</span>></span><span><<span>input</span><span>type</span>=<span>"submit"</span>></span><span></<span>form</span>></span><span><span><?php</span><span>$name</span> = <span>$_REQUEST</span>[<span>'fname'</span>]; <span>echo</span><span>$name</span>; <span>?></span></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
PHP $_POST
<code>PHP $_POST 广泛用于收集提交 <span><span>method</span>="<span>post</span>" 的 <span>HTML</span> 表单后的表单数据。$_<span>POST</span> 也常用于传递变量。 下面的例子展示了一个包含输入字段和提交按钮的表单。当用户点击提交按钮来提交数据后,表单数据会发送到 <<span>form</span>> 标签的 <span>action</span> 属性中指定的文件。在本例中,我们指定文件本身来处理表单数据。如果您希望使用另一个 <span>PHP</span> 页面来处理表单数据,请用更改为您选择的文件名。然后,我们可以使用超全局变量 $_<span>POST</span> 来收集输入字段的值:</span></code>
Example
<code><span><<span>html</span>></span><span><<span>body</span>></span><span><<span>form</span><span>method</span>=<span>"post"</span><span>action</span>=<span>"<?php echo $_SERVER['PHP_SELF'];?>"</span>> Name: <span><<span>input</span><span>type</span>=<span>"text"</span><span>name</span>=<span>"fname"</span>></span><span><<span>input</span><span>type</span>=<span>"submit"</span>></span><span></<span>form</span>></span><span><span><?php</span><span>$name</span> = <span>$_POST</span>[<span>'fname'</span>]; <span>echo</span><span>$name</span>; <span>?></span></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
PHP $_GET
<code>PHP $_GET 也可用于收集提交 HTML 表单 (<span><span>method</span>="<span>get</span>") 之后的表单数据。 $_<span>GET</span> 也可以收集 <span>URL</span> 中的发送的数据。</span></code>
Suppose we have a page containing a hyperlink with parameters:
<code><span><<span>html</span>></span><span><<span>body</span>></span><span><<span>a</span><span>href</span>=<span>"test_get.php?subject=PHP&web=W3school.com.cn"</span>></span>测试 $GET<span></<span>a</span>></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
<code>当用户点击链接 <span>"Test <span>$GET</span>"</span>,参数 <span>"subject"</span> 和 <span>"web"</span> 被发送到 <span>"test_get.php"</span>,然后您就能够通过 <span>$_GET</span> 在 <span>"test_get.php"</span> 中访问这些值了。 下面的例子是 <span>"test_get.php"</span> 中的代码: 实例</code>
<code><span><<span>html</span>></span><span><<span>body</span>></span><span><span><?php</span><span>echo</span><span>"Study "</span> . <span>$_GET</span>[<span>'subject'</span>] . <span>" at "</span> . <span>$_GET</span>[<span>'web'</span>]; <span>?></span></span><span></<span>body</span>></span><span></<span>html</span>></span></code>
The above has introduced 15 PHP global variables - super global variables, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.