Understand PHP variable scope

怪我咯
Release: 2023-03-10 20:20:02
Original
1238 people have browsed it

Variable scope is the context in which it is defined (that is, its effective scope). In JavaScript, there is no concept of variable scope. A similar concept may be scope. However, since JavaScript uses lexical scope, which refers to the position when the variable is declared; and PHP does not have variable declaration, the variable is equivalent to declaring the variable when it is assigned a value for the first time. Therefore, the two are not the same. This article will introduce the variable scope in PHP in detail

Range span

Most PHP variables have only a single scope. This separate scope span also includes files introduced by include and require

Here the variable $a will take effect in the included file b.inc

<?php
$a = 1;
include &#39;b.inc&#39;;
?>
Copy after login

Function scope

In user custom function, a local function scope will be introduced. Any variables used inside the function will be restricted to the local function scope by default

The following script will not produce any output because the echo statement refers to a local version of the variable $a, and in this scope, it has not been assigned a value

<?php
$a = 1; /* global scope */
function Test()
{
    echo $a; /* reference to local scope variable */
}
Test();
?>
Copy after login

global keyword

The output of the following script will be "3". After global variables a and

b are declared in a function, all references to either variable will point to its global version. PHP has no limit on the maximum number of global variables that can be declared by a function

<?php
$a = 1;
$b = 2;
function Sum()
{
    global $a, $b;
    $b = $a + $b;
}
Sum();
echo $b;//3
?>
Copy after login

The second way to access variables in the global scope is to use a special PHP custom $GLOBALS array

GLOBALS is an associative array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the content of the variable. The reason why GLOBALS exists in the global scope is because GLOBALS is a superglobal variable

<?php
$a = 1;
$b = 2;
function Sum()
{
    $GLOBALS[&#39;b&#39;] = $GLOBALS[&#39;a&#39;] + $GLOBALS[&#39;b&#39;];
}
Sum();
echo $b;//3
?>
Copy after login

predefined variable

For all For scripts, PHP provides a large number of predefined variables. These variables represent all external variables as built-in environment variables, and error messages as return headers

The following is a list of predefined variables

超全局变量 — 超全局变量是在全部作用域中始终可用的内置变量
$GLOBALS — 引用全局作用域中可用的全部变量
$_SERVER — 服务器和执行环境信息
$_GET — HTTP GET 变量
$_POST — HTTP POST 变量
$_FILES — HTTP 文件上传变量
$_REQUEST — HTTP Request 变量
$_SESSION — Session 变量
$_ENV — 环境变量
$_COOKIE — HTTP Cookies
$php_errormsg — 前一个错误信息
$HTTP_RAW_POST_DATA — 原生POST数据
$http_response_header — HTTP 响应头
$argc — 传递给脚本的参数数目
$argv — 传递给脚本的参数数组
Copy after login

Most of the predefined variables Variables are not superglobal variables, they need to use the 'global' keyword to make them valid in the local scope of the function

Superglobal variables

Superglobal variables are valid in any scope, They do not require a 'global' declaration

Below is a list of superglobal variables

$GLOBALS
$_SERVER
$_GET$_POST
$_FILES$_COOKIE
$_SESSION
$_REQUEST
$_ENV
Copy after login
<?php
function test_global()
{
    //预定义变量
    global $HTTP_POST_VARS;
    echo $HTTP_POST_VARS[&#39;name&#39;];
    //超全局变量
    echo $_POST[&#39;name&#39;];
}
?>
Copy after login


The above is the detailed content of Understand PHP variable scope. 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