Understand variable scope and superglobal variables in PHP

WBOY
Release: 2023-05-12 09:52:01
Original
1421 people have browsed it

In the process of learning and using PHP, variable scope and superglobal variables are two very important concepts. Only by fully understanding them can we make better use of them to realize the functions of the code.

First, let’s take a look at variable scope. In PHP, variable scope mainly includes global variables and local variables. Global variables refer to variables declared outside a function and can be used both inside and outside the function. Local variables refer to variables declared inside a function and can only be used inside the function.

Within a function, if you want to use a global variable, you need to declare it using the global keyword inside the function. For example:

$x = 5; // 全局变量

function myTest() {
    global $x;
    echo $x;
}
myTest(); // 输出:5
Copy after login

It should be noted that the global keyword can only be used to access global variables, but cannot create a global variable inside a function.

In addition to the global keyword, PHP also provides another keyword-$GLOBALS. $GLOBALS are called superglobal variables and can be used anywhere, both inside and outside functions. $GLOBALS is an array containing all global variables. We can access the elements in this array by adding $global before the variable name. For example:

$x = 5; // 全局变量

function myTest() {
    echo $GLOBALS["x"];
}
myTest(); // 输出:5
Copy after login

In the above code, $GLOBALS["x"] and $x are equivalent.

In addition to $GLOBALS, PHP also provides several other superglobal variables, the most commonly used of which are $_POST, $_GET and $_REQUEST. They are used to handle the HTTP POST and GET methods respectively and an array containing all HTTP request variables. For example:

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    Name: <input type="text" name="name">
    <input type="submit">
</form>
<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // 处理form提交的数据
        $name = $_POST['name'];
        echo $name;
    }
?>
Copy after login

In the above code, the first form will submit data to the current page, using $_SERVER['PHP_SELF'] to represent the address of the current page. Determine in the page if the request method is POST, get the value of the 'name' attribute from the $_POST array and output it.

It should be noted that the super-global variables $_SERVER, $_SESSION and $_COOKIE are also commonly used variables. They are used to access server-related information, store user-related information, and access client cookie information.

In short, it is very important to understand the variable scope and superglobal variables in PHP, because they allow us to use variables more flexibly and conveniently, and provide us with many convenient functions.

The above is the detailed content of Understand variable scope and superglobal variables in PHP. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!