[PHP] global variables: differences between global and $ globals and their usage

Source: Internet
Author: User
Tags parse error

When writing a framework today, I want to use saemysql as a global variable after initialization.
However, we found that the global variables in PHP and the global variables in Java or oC are quite different.
Next we will record the global usage precautions in PHP.
1. Global variables are required in some cases, as shown in the following example:

<? PHP $ name = "why"; // defines the variable name and initializes function echoname () {// tries to reference the variable echo "myname is" outside the function ". $ name. "<br>" ;}echoname () ;?>


The result of the above Code is: "myname is ". Instead of expecting: "myname is why ". 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.

2. Change the above Code

<? Phpglobal $ name = "why"; // value the function echoname () {// echo "myname is" variable outside the function ". $ name. "<br>" ;}echoname () ;?>

Result: Parse error: syntax error, unexpected '=', expecting ',' or ';' in http: \ xxxxxxx.com 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.

3. Change the above Code again:

<? Phpglobal $ name; $ name = "why"; // separate the global declaration from the value assignment function echoname () {// try to reference the variable echo "myname is" outside the function ". $ name. "<br>" ;}echoname () ;?>

However, the result is still "myname is" because 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 ).

4. Further modify the above Code:

<? PHP $ name = "why"; // defines the variable name and initializes function echoname () {// declares $ name through global, which is equivalent to passing the global $ name parameter; echo "myname is ". $ name. "<br>" ;}echoname () ;?>

The expected result is: "myname is why ".
The code above shows that global is used to pass parameters, rather than making the scope of variables global.

5. The following code proves this:

<? PHP $ name = "why"; // declare the variable $ name and initialize function echoname1 () {// use global in function echoname1 () to declare $ nameglobal $ name; echo "the first name is ". $ name. "<br>";} function echoname2 () {// The global statement is not used in function echoname2 () to declare $ nameecho "the second name is ". $ name. "<br>" ;}echoname1 (); echoname2 () ;?>

Result:

The first name is why
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:

<? PHP $ name = "why"; // define the variable name and initialize function echoname () {// reference the external variable echo "myname is" through the Global Array $ globals ". $ globals ['name']. "<br>" ;}echoname () ;?>

The result is myname is why.

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.