Understand the scope of variables
Scope refers to the range within which a variable can be used or visible in a script. PHP has 6 basic scoping rules:
Built-in super global variables can be used and visible anywhere in the script.
Constants, once declared, are globally visible; that is, they can be used inside and outside functions.
Global variables declared within a script are visible throughout the script, but not inside functions.
When a variable used inside a function is declared as a global variable, its name must be consistent with the name of the global variable.
A variable created inside a function and declared as static cannot be visible outside the function, but can maintain its value across multiple executions of the function
A variable created inside a function is local to the function, and when When the function terminates, the variable no longer exists.
$_GET and $_POST arrays and some other special variables have their own scoping rules. These are called super global variables, and they can be used and visible everywhere, including inside and outside functions. The complete list of super global variables is as follows:
$GLOBALS,所有全局变量数组(就像global关键字,这将允许在一个函数内部访问全局变量) $_SERVER,服务器环境变量数组 $_GET,通过GET方法传递给脚本的变量数组 $_POST,通过POST方法传递给该脚本的变量数组 $_COOKIE,cookie变量数组 $_FILES,与文件上传相关的变量数组 $_ENV,环境变量数组 $_REQUEST,所有用户输入的变量数组,包括$_GET、$_POST和$_COOKIE所包含的输入内容(但是不包括PHP4.3.0版本以后的$_FILES) $_SESSION,会话变量数组
The above is the content of understanding the scope of variables in php. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!