PHP中许多预定义的变量都是“超全局的”,这意味着他们在一个脚本的全部作用域中都可用。在函数或方法中无需执行global $variable;也可以访问他们
这些超全局变量为:
.GLOBALS
._SERVER
._REQUEST
._POST
._GET
._FILES
._ENV
._COOKIE
._SESSION
1.$GLOBALS-引用全局作用域中可用的全部变量
实例如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
$x = 2;
$y = 3;
function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
</body>
</html>
2.$GLOBALS-引用全局作用域中可用的全部变量
$_SERVER这种超全局变量保存关于报头,路劲和脚本位置信息
展示如何使用$_SERVER中的某些值
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
</body>
</html>
3.$_REQUEST-收集HTML表单提交数据
简单实例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name:<input type="text" name="fname">
<input type="submit">
</form>
<?php
$name = $_REQUEST['fname'];
echo $name;
?>
</body>
</html>
4.$_POST-获取表单数据
如下实例中,当用户点击提交之后,表单数据会被发送到标签的action属性中指定的文件中
实例如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name:<input type="text" name="fname">
<input type="submit">
</form>
<?php
$name = $_POST['fname'];
echo $name;
?>
</body>
</html>
5.$_GET-提交表单数据
带超链接
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="one.php?name1=苗帅&name2=jesse">点我</a>
</body>
</html>
获取页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
echo $_GET['name1'] . " is " . $_GET['name2'];
?>
</body>
</html>