PHP global variables: the difference and use of global and $GLOBALS

【Foreword】

   This article summarizes PHP global variables: the difference and use of global and $GLOBALS

 

【Introduction】

   (1) global is to pass parameters, not to make the scope of variables global.

            The correct usage of global is: "Introduce an external variable in a function. If the variable is not passed in through a parameter, then it is introduced through global." That is, when a function refers to an external variable, it can be used in the function The variable is declared through global, so that the variable can be used in the function ( equivalent to being passed in as a parameter, not a global variable )

   (2) $GLOBALS['var'] is the external global variable itself. And global $var is the same name reference or pointer to the outer $var.

 

 

【Details】

1. Some occasions require the appearance of global variables, such as the following examples:

<?php    
$name="why";//Define the variable name and initialize it    
function echoName()    {    
// Attempt to reference a variable outside the function    
echo "myname is ".$name."<br>";    
}    
echoName();    
?>    

  The result of the above code is: "myname is" . instead of the expected: "myname is why". Because the function does not pass the value of the parameter $name, the attempt to refer to the external variable will not succeed. Consider using global at this time

 

2. So change the above code to

 

<?php    
global $name="why";//Assignment with global declaration    
function echoName()    {    
// Attempt to reference a variable outside the function    
echo "myname is ".$name."<br>";    
}    
echoName();    
?>   

 

   结果为:Parse error: syntax error, unexpected '=', expecting ',' or ';' in http:\\xxxxxxx.com on line 2

That is, the above code is wrong. The reason is that you cannot assign a value to a variable while declaring it with global

 

3. Change the above code again:

 

<?php    
global $name;    
$name="why";//Separate global declaration from assignment    
function echoName()    {    
// Attempt to reference a variable outside the function    
echo "myname is ".$name."<br>";    
}    
echoName();    
?>    

 

   But the result is still: "myname is" because the usage of global is wrong.

   The correct usage of global is: "Introduce an external variable in a function. If the variable is not passed in through a parameter, then it is introduced through global." That is, when a function refers to an external variable, it can be used in the function The variable is declared through global, so that the variable can be used in the function (equivalent to being passed in as a parameter).

 

4. Then further change the above code:

 

<?php    
$name="why";//Define the variable name and initialize it    
function echoName()    {    
//Declare $name through global, which is equivalent to passing parameters    
global $name;    
echo "myname is ".$name."<br>";    
}    
echoName();    
?>    

 

   At this point, the expected result is obtained: "myname is why".

   The above code shows that global is to pass parameters, not to make the scope of variables global.

 

5. The following code demonstrates this:

<?php    
$name="why";//Declare variable $name and initialize    
function echoName1()    {    
//在函数echoName1()里使用global来声明$name    
global  $name;    
echo "the first name is ".$name."<br>";    
}    
function echoName2()    {    
//在函数echoName2()里没有使用global来声明$name    
echo "the second name is ".$name."<br>";    
}    
echoName1();    
echoName2();    
?>    

   结果为:

   the first name is why

   the second name is

 

   上面的结果说明在函数echoName2()中,$name变量仍然是未知的,因为没有用global来声明,也就没有传递进去。同时也证明了global的作用并不是使变量的作用域为全局。

 

   当然,除了通过上述方法外,还可以使用全局数组$GLOBALS来解决问题,在需要用到外部变量的地方,使用$GLOBALS['var']就可以了。例:

<?php        
$name="why";//定义变量name,并初始化    
function echoName()    {    
//通过全局数组$GLOBALS来引用外部变量    
echo "myname is ".$GLOBALS['name']."<br>";    
}    
echoName();    
?>  

得到的结果为:   myname is why 

 

【总结】

   综上,global的作用就相当于传递参数,在函数外部声明的变量,如果在函数内想要使用,就用global来声明该变量,这样就相当于把该变量传递进来了,就可以引用该变量了。

 

 

 

 

 

 

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326152493&siteId=291194637