什么是php超全局变量数组

PhpPhp 2023-08-28 22:41:58 757
摘要: PHP超级全局变量数组(SuperGlobalArray),又称为PHP预定义数组,是由PHP引擎内置的,不需要开发者重新定义。在PHP脚本运行时,PHP会自动将一些数据放在超级全局数组中。1、$_GET数组用于收集通过GET方法提交的表单数据,通...

PHP超级全局变量数组(Super Global Array),又称为PHP预定义数组,是由PHP引擎内置的,不需要开发者重新定义。 在PHP脚本运行时,PHP会自动将一些数据放在超级全局数组中。

1、$_GET 数组用于收集通过 GET 方法提交的表单数据,通常以 URL 参数形式出现

<form action="process.php" method="get">
  Name: <input type="text" name="name"><br>
  Age: <input type="text" name="age"><br>
  <input type="submit">
</form>
// process.php
$name = $_GET['name'];
$age = $_GET['age'];
echo "Welcome $name! You are $age years old.";

2、$_POST 数组用于收集通过 POST 方法提交的表单数据,可用于处理敏感数据

<form action="process.php" method="post">
  Name: <input type="text" name="name"><br>
  Age: <input type="text" name="age"><br>
  <input type="submit">
</form>
// process.php
$name = $_POST['name'];
$age = $_POST['age'];
echo "Welcome $name! You are $age years old.";

3、$_REQUEST 数组包含了GET、_POST、$_COOKIE 的内容。可以用来收集 HTML 表单提交后的数据或者从浏览器地址栏中获取数据。

<form action="process.php" method="post">
  Name: <input type="text" name="name"><br>
  Age: <input type="text" name="age"><br>
  <input type="submit">
</form>
// process.php
$name = $_REQUEST['name'];
$age = $_REQUEST['age'];
echo "Welcome $name! You are $age years old.";

4、$_COOKIE 数组用于访问已在客户端计算机上存储的 cookie

// send_cookie.php
setcookie('username', 'John', time() + (86400 * 30), "/"); // 设置 cookie
echo 'Cookie sent.