What are superglobal variables in What are superglobal variables in php? how to use?? how to use? variables can be roughly divided into global variables and local variables. The simple difference is that local variables are defined in the function and can only be used within the function. Global variables are defined outside the function and can be referenced and changed anywhere, but There is another kind of variable in PHP called super global variable. This article will introduce to you the meaning and usage of PHP super global variable.
What are What are superglobal variables in What are superglobal variables in php? how to use?? how to use? super global variables?
When using global variables in PHP functions, we need to use the global keyword to declare the content used, but if there is no variable declared with the global keyword, we call it a super global variable.
Super global variables are variables defined in PHP and cannot be added arbitrarily by users. They mainly store values received from the Web server.
Let’s take a lookThe types and formats of super global variables
There are currently nine types of super global variables in What are superglobal variables in What are superglobal variables in php? how to use?? how to use?
1、$GLOBALS 2、$_SERVER 3、$_GET 4、$_POST 5、$_FILES 6、$_COOKIE 7、$_SESSION 8、$_REQUEST 9、$_ENV
These are Associative arrays, so they are used like other arrays.
For example, if you access the URL https://server.com/user.What are superglobal variables in What are superglobal variables in php? how to use?? how to use??id=123, use $ _GET $ _GET ["id"], you can get "123".
Let’s take a look at specific examples
The code is as follows
<?What are superglobal variables in What are superglobal variables in php? how to use?? how to use? session_start(); if (isset($_REQUEST['clear'])) { session_destroy(); header('Location: '.$_SERVER['DOCUMENT_URI'], true, 301); } if (isset($_REQUEST['name'])) { $list = $_SESSION['list'] ?? []; $list[] = $_REQUEST['name']; $_SESSION['list'] = $list; } $_SESSION['count'] = intval($_SESSION['count'] ?? 0)+1; ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>超全局变量</title> </head> <body> <p><?= $_SESSION['count'] ?>输入内容</p> <form method="post"> <input type="text" name="name"> <button type="submit">提交</button> <a href="?clear">清除</a> </form> <ul> <?What are superglobal variables in What are superglobal variables in php? how to use?? how to use? if (isset($list)) { foreach ($list as $name) { ?><li><?= $name ?></li><?What are superglobal variables in What are superglobal variables in php? how to use?? how to use? } } ?> </ul> </body> </html>
The running results are as follows
This article ends here. For more exciting content, you can pay attention to other related columns on the What are superglobal variables in What are superglobal variables in php? how to use?? how to use? Chinese website for further study! ! !
The above is the detailed content of What are superglobal variables in php? how to use?. For more information, please follow other related articles on the PHP Chinese website!