Hello, everyone. Meet again. I'm Quan dujun. I've prepared the Idea registration code for you today.
Today, when writing the framework, I want to use SAE MySQL as a global variable after initialization. However, it was later found that the global variables in PHP are quite different from those in Java or OC. Here are some notes about the use of global in php. 1. Global variables are required on some occasions, as shown in the following examples:
<?php $name="why";//Define the variable name and initialize it function echoName() { //An attempt was made to reference a variable outside a function echo "myname is ".$name."<br>"; } echoName(); /* He asked about 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 will not succeed. Consider using global at this time.
2. The above code is changed to
<?php global $name="why";//Simultaneous assignment with global declaration function echoName() { //An attempt was made to reference a variable outside a function echo "myname is ".$name."<br>"; } echoName(); /* He asked about hovertree com */ ?>
The results are: Parse error: syntax error, unexpected '=', expecting ',' or ';' in http:\xxxxxxx.com on line 2 That is, there is an error in the above code. 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 global declarations from assignments function echoName() { //An attempt was made to reference a variable outside a function echo "myname is ".$name."<br>"; } echoName(); ?>
However, the result is still "myname is", because the usage of global is wrong.
The correct usage of global is: "introduce an external variable into a function. If the variable is not passed in through parameters, it will be introduced through global." That is, when a function references an external variable, the variable can be declared in the function through global, so that the variable can be used in the function (equivalent to being passed in as a parameter).
4. The above code was further changed:
<?php $name="why";//Define the variable name and initialize it function echoName() { //Declaring $name through global is equivalent to passing parameters global $name; echo "myname is ".$name."<br>"; } echoName(); /* He asked about hovertree com */ ?>
At this point, you get the desired result: "myname is why". The above code shows that global is used to pass parameters, not to make the scope of variables global.
5. The following code proves this:
<?php $name="why";//Declare the variable $name and initialize it function echoName1() { //Use global to declare $name in the function echoName1() global $name; echo "the first name is ".$name."<br>"; } /* He asked about hovertree com */ function echoName2() { //global is not used to declare $name in the function echoName2() 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 the function echoName2(), the $name variable is still unknown, because it is not declared with global, so it is not passed in. It is also proved that the role of global is not to make the scope of variables global.
To sum up, the function of global is equivalent to passing parameters. For variables declared outside the function, if you want to use them in the function, use global to declare the variable, which is equivalent to passing in the variable and referencing the variable.
Of course, in addition to the above methods, you can also use the global array GLOBALS to solve the problem. Where external variables need to be used, you can use GLOBALS ['var']. Example:
<?php $name="why";//Define the variable name and initialize it function echoName() { //External variables are referenced through the global array $GLOBALS echo "myname is ".$GLOBALS['name']."<br>"; } echoName(); ?>
The result is: myname is why.