Analysis of global and $globals[in PHP

Source: Internet
Author: User
Tags parse error
Analysis of global and $globals[in PHP
Global issues
The use of global in PHP. Here's an example:
$name = "even";//define variable name, and initialize
function Echoname ()
{
Attempting to reference a variable outside a 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, an attempt to reference an external variable does not succeed. Consider using global at this time.

The above code is then changed to
Global $name = "even";//Assign value at the same time as global declaration
function Echoname ()
{
Attempting to reference a variable outside a function
echo "MyName is". $name. "
";
}
Echoname ();
?>
The result is: Parse error:syntax error, unexpected ' = ', expecting ', ' or '; ' in D:\phpserver\www\test\test.php to line 2
That is, the code above has errors. The reason is that you cannot assign a value to a variable while declaring it with global.
Change the above code again:
Global $name;
$name = "even";//separate the global declaration from the assignment
function Echoname ()
{
Attempting to reference a variable outside a function
echo "MyName is". $name. "
";
}
Echoname ();
?>
But the result remains: "MyName is".
The reason is that global does not use it, and the correct usage of global is: "Introduce an external variable into a function, and if the variable is not passed through a parameter, it is introduced through global." "That is, when a function refers to an external variable, it can be declared within a function through global, so that the variable can be used in the function (equivalent to passing in as a parameter)."

The above code is further changed:
$name = "even";//define variable name, and initialize
function Echoname ()
{
Declaring $name with global, equivalent to passing parameters
Global $name;
echo "MyName is". $name. "
";
}
Echoname ();
?>
At this point the desired result is obtained: "MyName is even".

The code above shows that global is acting as a pass-through parameter, rather than making the scope of the variable global. This is demonstrated by the following code:
$name = "even";//declare variable $name, and initialize
function echoName1 ()
{
Use Global in function echoName1 () to declare $name
Global $name;
echo "The first name is". $name. "
";
}
function EchoName2 ()
{
The function echoName2 () does not use global to declare $name
echo "The second name is". $name. "
";
}
EchoName1 ();
EchoName2 ();
?>

The result is:
The first name is even
The second name is
The above results show that in function echoName2 (), the $name variable is still unknown because it is not declared with global and is not passed in. It is also proved that global does not make the scope of the variable global.

In summary, the role of global is equivalent to passing parameters, variables declared outside the function, if you want to use within the function, you declare the variable with global, so it is equivalent to pass the variable in, you can reference the variable.

Of course, in addition to the above method, you can also use the global array $globals to solve the problem, where you need to use the external variables, the use of $globals[' var '. Cases:
$name = "even";//define variable name, and initialize
function Echoname ()
{
Referencing external variables with global array $globals
echo "MyName is". $GLOBALS [' name ']. "
";
}
Echoname ();
?>
The results are as follows: MyName is even.
In addition, when using global and $globals, be aware of the case, if the case is wrong, the keyword will not work.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.