Global variables: Difference and use between global and $globals

Source: Internet
Author: User
Tags parse error

Today, when writing the framework, I want to use the Saemysql as a global variable after initializing it.
But later found that the global variables in PHP and Java or OC in the global variables are still a big difference.
The following is a note about the use of global in PHP.
1. There are situations where global variables are required, such as the following example:

<? PHP   $name= "why"; // define the variable name and initialize  the function Echoname ()  {  // try to refer to the 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, an attempt to reference an external variable does not succeed. Consider using global at this time.

2. Then change the code above to

<? PHP   Global $name= "why"; // assigning  values at the same time as global declaration function Echoname ()  {  // try to refer to the variable  outside the function echo "MyName is".  $name. " <br> ";  }  Echoname ();   /*  */?>  

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

3. Change the above code again:

<? PHP   Global $name ;   $name= "why"; // separating  the global declaration from the assignment function Echoname ()  {  // try to refer to the variable  outside the function echo "MyName is".  $name. " <br> ";  }  Echoname ();  ? >  

But the result remains: "MyName is", because global is not in the right use. The correct use of

Global is to "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)."

4. Further changes to the above code:

<? PHP   $name= "why"; // define the variable name and initialize  the function Echoname ()  {  // declare $name via global, equivalent to pass parameter   global$name;   Echo "MyName is". $name. " <br> ";  }  Echoname ();   /*  */?>

At this point we get the desired result: "MyName is why".
The code above shows that global is acting as a pass-through parameter, rather than making the scope of the variable global.

5. This is demonstrated by the following code:

<?PHP$name= "why";//declares the variable $name and initializesfunctionechoName1 () {//use Global in function echoName1 () to declare $nameGlobal  $name; Echo"The first name is".$name." <br> "; }  /*how to ask Hovertree.com*/functionechoName2 () {//the function echoName2 () does not use global to declare $nameEcho"The second name is".$name." <br> ";  } echoName1 ();  EchoName2 (); ?>

The result is:

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

<? PHP     $name= "why"; // define the variable name and initialize  the function Echoname ()  {  // using the global array $globals to refer to  the external variable echo "MyName is".  $GLOBALS[' name ']. <br> ";  }  Echoname ();  ? >  

The results are as follows: MyName is.

Recommendation: http://www.cnblogs.com/roucheng/p/phpshuzu.html

Global variables: Difference and use between global and $globals

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.