Analysis of global and $ GLOBALS [] in php

Source: Internet
Author: User
Global usage in php. Example: & amp; lt ;? Php $ name & amp; quot; even & amp; quot; define the variable name and initialize functionechoName () {echo & amp; quot; my... global problems
Use of global in php. Example:
$ Name = "even"; // defines the variable name and initializes it.
Function echoName ()
{
// Try to reference variables outside the function
Echo "myname is". $ name ."
";
}
EchoName ();
?>
Analysis:
The result of the above code is: "myname is ". Instead of expecting: "myname is even ". Because the function does not pass the value of $ name, an attempt to reference an external variable will not succeed. In this case, use global.

Change the above code
Global $ name = "even"; // assign values when declared by global
Function echoName ()
{
// Try to reference variables outside the function
Echo "myname is". $ name ."
";
}
EchoName ();
?>
Result: Parse error: syntax error, unexpected '=', expecting ',' or ';' in D: \ phpserver \ www \ test. php on line 2
That is, the above code has an error. The reason is that you cannot assign values to variables while declaring variables using global.
Change the above code again:
Global $ name;
$ Name = "even"; // separate the global declaration from the value assignment.
Function echoName ()
{
// Try to reference variables outside the function
Echo "myname is". $ name ."
";
}
EchoName ();
?>
However, the result is still "myname is ".
The reason is that the usage of global is incorrect. the correct usage of global is: "An external variable is introduced into a function. if the variable is not passed in through a parameter, it is introduced through global. "That is, when a function references an external variable, the variable can be declared in the function through global, in this way, the variable can be used in the function (equivalent to being passed in as a parameter ).

The above code is further modified:
$ Name = "even"; // defines the variable name and initializes it.
Function echoName ()
{
// Declare $ name through global, which is equivalent to passing parameters
Global $ name;
Echo "myname is". $ name ."
";
}
EchoName ();
?>
The expected result is "myname is even ".

The code above shows that global is used to pass parameters, rather than making the scope of variables global. The following code demonstrates this:
$ Name = "even"; // declare the variable $ name and initialize
Function echoName1 ()
{
// Use global in the function echoName1 () to declare $ name
Global $ name;
Echo "the first name is". $ name ."
";
}
Function echoName2 ()
{
// The $ name is not declared using global in the function echoName2 ().
Echo "the second name is". $ name ."
";
}
EchoName1 ();
EchoName2 ();
?>

Result:
The first name is even
The second name is
The above results show that in the function echoName2 (), the $ name variable is still unknown because it is not declared using global, and is not passed in. It also proves that the role of global is not to make the scope of the variable global.

In summary, the role of global is equivalent to passing parameters. variables declared outside the function are declared using global if they are to be used in the function, this is equivalent to passing the variable in, and 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. you can use $ GLOBALS ['var'] where external variables are needed. Example:
$ Name = "even"; // defines the variable name and initializes it.
Function echoName ()
{
// Reference external variables through 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 case sensitivity issue. if the case is incorrect, 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.