What are superglobal variables in php? how to use?

不言
Release: 2023-04-04 14:50:02
Original
5183 people have browsed it

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 superglobal variables in What are superglobal variables in php? how to use?? how to use?

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
Copy after login

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[&#39;clear&#39;])) {
    session_destroy();
    header(&#39;Location: &#39;.$_SERVER[&#39;DOCUMENT_URI&#39;], true, 301);
}
if (isset($_REQUEST[&#39;name&#39;])) {
    $list = $_SESSION[&#39;list&#39;] ?? [];
    $list[] = $_REQUEST[&#39;name&#39;];
    $_SESSION[&#39;list&#39;] = $list;
}
$_SESSION[&#39;count&#39;] = intval($_SESSION[&#39;count&#39;] ?? 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[&#39;count&#39;] ?>输入内容</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>
Copy after login

The running results are as follows

What are superglobal variables in What are superglobal variables in php? how to use?? how to use?

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template