Understand the scope of variables in php

PHP中文网
Release: 2016-07-25 08:45:57
Original
1012 people have browsed it

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,会话变量数组
Copy after login

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)!


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