Home >Backend Development >PHP Tutorial >Analysis of global and $GLOBALS[] in php
Global problem
The use of global in php. The following example:
$name="even";//Define variable name and initialize
function echoName()
{
//Trying to reference variables outside the function
echo "myname is ".$name. "
";
}
echoName();
?>
Analysis:
The result of the above code is: "myname is". Instead of the expected: "myname is even". Because the function does not pass the value of the parameter $name, trying to reference external variables will not succeed. Consider using global at this time.
So the above code was changed to
global $name="even";//Assign value while declaring with global
function echoName()
{
//Trying to reference variables outside the function
echo "myname is ".$name."
";
}
echoName();
?>
The result is: Parse error: syntax error, unexpected '=', expecting ',' or ';' in D: phpserverwwwtesttest.php on line 2
That is, there is an error in the above code. The reason is that you cannot assign a value to a variable while declaring it global.
Change the above code again:
global $name;
$name="even";//Separate global declaration from assignment
function echoName()
{
//Trying to reference variables outside the function
echo "myname is ".$name."
";
}
echoName();
?>
But the result is still: "myname is".
The reason is that the usage of global is incorrect. The correct usage of global is: "Introducing an external variable into a function. If the variable is not passed in through parameters, then it is introduced through global." In other words, when a function references When it is an external variable, you can declare the variable through global within the function, so that the variable can be used in the function (equivalent to passing it in as a parameter).
So we further changed the above code:
$name="even";//Define the variable name and initialize it
function echoName()
{
//Declaring $name through global is equivalent to passing parameters
global $name;
echo "myname is ".$name."
";
}
echoName();
?>
The expected result is obtained at this time: "myname is even ".
The above code shows that global is used to pass parameters, rather than making the scope of the variable global. The following code proves this:
$name="even";//Declare the variable $name and initialize it
function echoName1()
{
//Use global to declare $ in the function echoName1() name
global $name;
echo "the first name is ".$name."
";
}
function echoName2()
{
//Global is not used to declare $name in the function echoName2()
echo "the second name is ".$name."
";
}
echoName1();
echoName2();
?>
The result is:
the first name is even
the second name is
The above result shows that in the function echoName2(), the $name variable is still unknown, because it is not declared with global and is not passed in. It also proves that the role of global is not to make the scope of the variable global.
To sum up, the role of global is equivalent to passing parameters. If you want to use a variable declared outside the function, use global to declare the variable. This is equivalent to passing the variable in, and you can The variable is referenced.
Of course, in addition to the above methods, you can also use the global array $GLOBALS to solve the problem. Where external variables need to be used, just use $GLOBALS['var']. Example: t & lt;? PHP
$ name = "EVEN"; // Define the variable name, and initialize
Function echoname () {
// via the global array $ globals
echo "myname is". $ GLOBALS['name']."
";
}
echoName();
?>
The result is: myname is even .
In addition, when using global and $GLOBALS, pay attention to the capitalization issue. If the capitalization is wrong, the keyword will not work.