Home > Backend Development > PHP Tutorial > Some different understandings of global and $GLOBAL['']

Some different understandings of global and $GLOBAL['']

伊谢尔伦
Release: 2023-03-04 20:52:02
Original
2047 people have browsed it

Global variables in PHP must be declared as global when used in a function (the keyword Global is only useful when defined in a function).

The function of Global is to define global variables, but this global variable does not apply to the entire website, but to the current page, including all files in include or require.

<?PHP 
$a=123; 
function aa() 
{ 
Global $a; 
//如果不把$a定义为global变量,函数体内是不能访问函数体外部的$a的,但是可以定义一个相同的名字$a,
//此时这个变量是局部变量,等同于C语言的局部变量,只能在函数体内部使用。 
echo $a; 
} 
aa(); 
?>
Copy after login

Global variables defined in the function body can be used outside the function body, but global variables defined outside the function body cannot be used inside the function body.

$global $a; 
$a=123; 
function f() 
{ 
echo $a; //错误, 
} 
//再看看下面一例 
function f() 
{ 
global $a; 
$a=123; 
} 
f(); 
echo $a; //正确,可以使用
Copy after login

Example comparison:

<?php 
$var1 = 1; 
function test(){ 
unset($GLOBALS[&#39;var1&#39;]); 
} 
test(); 
echo $var1; 
?>
Copy after login

Because $var1 was deleted, nothing was printed.

<?php 
$var1 = 1; 
function test(){ 
global $var1; 
unset($var1); 
} 
test(); 
echo $var1; 
?>
Copy after login

Accidentally printed 1. It proves that only the alias reference is deleted, and its value is not changed in any way.

global $var is actually &$GLOBALS['var'], which is just an alias for calling external variables.
$var1 and $GLOBALS['var1'] in the above code refer to the same variable, not two different variables.
PHP’s global variables are a little different from C language. In C language, global variables automatically take effect in functions unless covered by local variables. This can cause problems, as someone might carelessly mutate a global variable. Global variables in PHP must be declared as global using global when used in functions.
The function of PHP's Global variable is to define global variables, but this global variable does not apply to the entire website, but to the current page, including all files in include or require.

Quoting the explanation of $GLOBALS in the PHP manual:

Global variable: $GLOBALS, note: $GLOBALS is applicable in PHP 3.0.0 and later versions .

An array consisting of all defined global variables. The variable name is the index into the array. This is a "superglobal", or can be described as an automatic global variable.

That is to say, $var1 and $GLOBALS['var1'] in the above code refer to the same variable, not 2 different variables!

If a reference is assigned to a variable declared as global inside a function, the reference is only visible inside the function. This can be avoided by using the $GLOBALS array.

We all know that the variables generated by functions in PHP are private variables of the function, so the variables generated by the global keyword certainly cannot escape this rule. global generates an alias in the function that points to the external variable of the function. variables, rather than real variables external to the function. Once the pointing address of the alias variable is changed, some unexpected situations will occur. $GLOBALS[] is indeed called an external variable, and it will always remain consistent inside and outside the function.

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

The output will be “3″. Global variables $a and $b are declared in the function, and all reference variables of any variable will point to the global variables.

Why is it not 2? Doesn’t it have no effect outside the function? Please note that $b is not modified by reference in the function, but the modified $b points to the value of physical memory, so the external input is 3.

So we come to the conclusion that the difference between global and $GLOBALS[] in a function is:

global generates an alias variable in the function that points to the external variable of the function, not the real function For external variables, once the address pointed to by the alias variable is changed, some unexpected situations will occur.

$GLOBALS[] is indeed called an external variable, and it will always be consistent inside and outside the function!

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