PHP 4.1.0에 도입된 슈퍼전역 변수는 모든 범위에서 항상 사용할 수 있는 내장 변수입니다.
<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>
예
PHP $_SERVER
<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>
예
다음 표에는 $_SERVER에서 액세스할 수 있는 가장 중요한 요소가 나열되어 있습니다.<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>
PHP $_REQUEST
PHP $_REQUEST는 HTML 양식으로 제출된 데이터를 수집하는 데 사용됩니다.
인스턴스
<code>下面的例子展示了一个包含输入字段及提交按钮的表单。当用户通过点击提交按钮来提交表单数据时, 表单数据将发送到 <form> 标签的 action 属性中指定的脚本文件。在这个例子中,我们指定文件本身来处理表单数据。如果您需要使用其他的 PHP 文件来处理表单数据,请修改为您选择的文件名即可。然后,我们可以使用超级全局变量 <span>$_REQUEST</span> 来收集 input 字段的值:</code>
<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>
<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>
<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>
<code>PHP $_GET 也可用于收集提交 HTML 表单 (<span><span>method</span>="<span>get</span>") 之后的表单数据。 $_<span>GET</span> 也可以收集 <span>URL</span> 中的发送的数据。</span></code>
위에서는 내용의 측면을 포함하여 15개의 PHP 전역 변수(슈퍼 전역 변수)를 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.
<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>