©
このドキュメントでは、 php中国語ネットマニュアル リリース
超全局变量 — 超全局变量是在全部作用域中始终可用的内置变量
PHP 中的许多预定义变量都是“超全局的”,这意味着它们在一个脚本的全部作用域中都可用。在函数或方法中无需执行 global $variable; 就可以访问它们。
这些超全局变量是:
版本 | 说明 |
---|---|
4.1.0 | 超全局变量被引入到 PHP. |
Note: 变量可用性
默认情况下,所有的超全局变量都是可用的。但是,有一些指令会影响这种可用性。更多信息,参见文档 variables_order.
Note: 处理 register_globals
如果已经弃用的 register_globals 指令被设置为 on 那么局部变量也将在脚本的全局作用域中可用。例如, $_POST['foo'] 也将以 $foo 的形式存在。
相关信息,参见 FAQ “register_globals 对我有什么影响?”
Note: 可变变量
在函数或类方法中,超全局变量不能被用作可变变量。
[#1] John Slegers [2013-11-18 21:35:16]
Want to know how to generate a formatted list with all globals (including custom ones)? Check out the code below.
<?php
// Generate a formatted list with all globals
//----------------------------------------------------
// Custom global variable $_CUSTOM
$_CUSTOM = array('USERNAME' => 'john', 'USERID' => '18068416846');
// List here whichever globals you want to print
// This could be your own custom globals
$globals = array(
'$_SERVER' => $_SERVER, '$_ENV' => $_ENV,
'$_REQUEST' => $_REQUEST, '$_GET' => $_GET,
'$_POST' => $_POST, '$_COOKIE' => $_COOKIE,
'$_FILES' => $_FILES, '$_CUSTOM' => $_CUSTOM
);
?>
<html>
<style>
<?php // Adjust CSS formatting for your output ?>
.left {
font-weight: 700;
}
.right {
font-weight: 700;
color: #009;
}
.key {
color: #d00;
font-style: italic;
}
</style>
<body>
<?php
// Generate the output
echo '<h1>Superglobals</h1>';
foreach ($globals as $globalkey => $global) {
echo '<h3>' . $globalkey . '</h3>';
foreach ($global as $key => $value) {
echo '<span class="left">' . $globalkey . '[<span class="key">\'' . $key . '\'</span>]</span> = <span class="right">' . $value . '</span><br />';
}
}
?>
</body>
</html>
[#2] kitchin [2013-05-14 18:09:24]
Since PHP 5.4, you cannot use a superglobal as the parameter to a function. This causes a fatal error:
function foo($_GET) {
// whatever
}
It's called "shadowing" a superglobal, and I don't know why people ever did it, but I've seen it out there. The easy fix is just to rename the variable $get in the function, assuming that name is unique.
There was no deprecation warning issued in previous versions of PHP, according to my testing, neither in 5.3 nor 5.2. The error messages in 5.4 are:
Fatal error: Cannot re-assign auto-global variable _GET in...
Fatal error: Cannot re-assign auto-global variable _COOKIE in...
etc.
[#3] serpent at paradise dot net dot nz [2012-07-16 01:27:38]
You can go the other way as well i.e.
<?php
function test() {
$GLOBALS['foo'] = "Example content";
}
test();
echo "<p>$foo</p>";
?>
This doesn't appear to be affected by register_globals, I have it switched off.
[#4] lskatz at gmail dot com [2008-10-10 08:51:14]
Tibor:
It's not a good idea to use $_ENV unless you are specifying an environmental variable. This is probably a better example that I found on another page in php.net
<?php
function test() {
$foo = "local variable";
echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
echo '$foo in current scope: ' . $foo . "\n";
}
$foo = "Example content";
test();
?>