Home >Backend Development >PHP Tutorial >About global variables in PHP

About global variables in PHP

WBOY
WBOYOriginal
2016-10-22 00:14:261235browse

Hello everyone, I read the difference between the $_GLOBALS super global array and the global variable defined by global from the Internet. The difference is as follows:

  1. $GLOBALS['var'] is the external global variable itself

  2. global $var is a reference or pointer to an external $var variable with the same name, and is not a real assignment

So I tried it, the code is as follows:

<code><?php 
$var1 = 1; 
function test(){ 
    global $var1;                       //变量的引用
    unset($GLOBALS['var1']);            //销毁变量本身
    echo $var1;
} 

test(); 
?></code>

According to Xiaobai's thinking, this is this: Since it has been unset($GLOBALS['var1']);, the variable itself is destroyed, and the reference to the variable has no meaning, so the result cannot be output.

But I ran it, and the result was 1, so I couldn’t understand it as a novice. I would like to ask someone to explain it to me. Thank you in advance!

Reply content:

Hello everyone, I read the difference between the $_GLOBALS super global array and the global variable defined by global from the Internet. The difference is as follows:

  1. $GLOBALS['var'] is the external global variable itself

  2. global $var is a reference or pointer to an external $var variable with the same name, and is not a real assignment

So I tried it, the code is as follows:

<code><?php 
$var1 = 1; 
function test(){ 
    global $var1;                       //变量的引用
    unset($GLOBALS['var1']);            //销毁变量本身
    echo $var1;
} 

test(); 
?></code>

According to Xiaobai's thinking, this is this: Since it has been unset($GLOBALS['var1']);, the variable itself is destroyed, and the reference to the variable has no meaning, so the result cannot be output.

But I ran it, and the result was 1, so I couldn’t understand it as a novice. I would like to ask someone to explain it to me. Thank you in advance!

In PHP, the function inside is always a private variable. Global generates an alias variable in the function that points to the external variable of the function, rather than a simple reference or pointer to the external variable with the same name

In fact, it can be simply understood as the problem of the address pointer of the variable.

  1. $GLOBALS['var1'] and the external $var1 are the same pointer, pointing to the memory address where the value 1 is stored

  2. global $var1 is a copy pointer of the external $val1 pointer, also pointing to the memory address where the value 1 is stored

  3. No matter $GLOBALS['var1'] or global $var1, because the value memory address pointed to is the same, the purpose of modifying the value of the external variable can be obtained.
    So:

unset($GLOBALS['var1']) The operation also destroys the external $var1
test function global $val1; unset($val1) will not destroy the external $var1
Look at the code below, you will have a clearer view

<code><?php 
$var1 = 1; 
function test(){ 
    global $var1;
    unset($var1);                     
} 
test(); 
echo $var1;// 此处输出1</code>
<code><?php 
$var1 = 1; 
function test(){ 
    unset($GLOBALS['var1']);
} 
test(); 
echo $var1;// 此处报错PHP Notice:  Undefined variable: var1</code>

<code>global $var1;
</code>

is approximately equal to

<code>$var1 = $GLOBALS['var1'];</code>
Statement:
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