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  function echoname ()  {  //try to reference the variable outside the function  echo "MyName is". $ Name. " <br> ";  }  Echoname ();  /* Why ask Hovertree.com */?>  

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";//Assign Function  Echoname () with global Declaration ()  {  //try to refer to the variable outside of the functions  echo " MyName is ". $name." <br> ";  }  Echoname ();  /* Why ask Hovertree.com */?>  

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";//separate global Declaration and assignment  function echoname ()  {  //try to reference a variable outside the function  echo "MyName is". $name. " <br> ";  }  Echoname ();  ? >  

However, the result is still: "MyName is", because the use of global is not correct.

The correct usage for global is: "Introduce an external variable into a function, and if the variable is not passed through the 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 are made to the above code:

<?php  $name = "why";//define the variable name and initialize  function echoname ()  {  //through Global to declare $name, which is equivalent to passing parameters  Global $name;  echo "MyName is". $name. " <br> ";  }  Echoname ();  /* Why ask Hovertree.com */?>

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";//declare variable $name and initialize  function echoName1 ()  {  //Use Global in function echoName1 () to declare $name  global  $name;  echo "The first name is". $name. " <br> ";  }  /* Why ask Hovertree.com */function echoName2 ()  {  //in function echoName2 () does not use global to declare $name  echo "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 variable name and initialize  function echoname ()  {  //global array $globals to refer to external variable  echo " MyName is ". $GLOBALS [' name ']." <br> ";  }  Echoname ();  ? >  

The results are as follows: MyName is.

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.