The variables defined by global are global variables, so to speak, if a variable in a function is not a variable in PHP with the same name as the outside of the function, if we use global to define the same variable name in the function, then they are a variable. Let's take a few examples to illustrate.
For beginners in PHP, when using the global keyword, you might find that a variable outside of a function within a function does not output the variable correctly in some cases (that is, the global variable is invalid). Let's look at a simple and common example.
Here we have two pages of a.php and b.php.
The b.php page code is as follows:
The code is as follows |
Copy Code |
$site _name = ' Codeplayer '; function Sayhi () { Global $site _name; echo "hello! Welcome to $site _name! "; } ?> |
The a.php page code is as follows:
The code is as follows |
Copy Code |
function Include_view_page () { Include ' b.php '; Sayhi (); } Include_view_page (); ?> |
The example above is very simple and we want to be able to display the welcome statement correctly when we visit the a.php page. However, unfortunately, when we use the browser to access the a.php page, we find that the output is as follows:
Hello! Welcome to!
That is, when we call the Sayhi () function in function include_view_page (), the $site_name of global in the b.php page Sayhi () function is not recognized correctly and takes effect. What the hell is going on here?
In fact, when we include b.php pages in function Include_view_page (), the variable $site_name of the b.php page is equivalent to the scope stored within the Include_view_page () function. It is well known that within a function global a variable actually establishes a reference to the page global variable within the function. In our case, this $site_name variable is only a local variable within the Include_view_page () function for a.php, so it is not possible to global the variable, and we cannot get the correct variable and variable value when we make the related call.
In PHP, we especially need to be aware of the problem of having a page in a function like the one above that causes the scope of variables in that page to change. To avoid this situation, we should try to minimize the multiple-level include call and try not to use include within the function. In addition, we can declare $site_name in the form of a global variable on the b.php page.
The code is as follows |
Copy Code |
b.php Global $site _name; $site _name = ' Codeplayer '; function Sayhi () { Global $site _name; echo "hello! Welcome to $site _name! "; } ?> |
example, referencing global variables within a function
Let's look at the following code:
The code is as follows |
Copy Code |
$var 1 = "#####"; $var 2 = "&&&&&"; function global_references ($use _globals) { Global $var 1, $var 2; if (! $use _globals) { $var 2 =& $var 1; 1 } else { $GLOBALS ["Var2"] =& $var 1; 2 } } Global_references (FALSE); echo "VAR2 is set to ' $var 2 ' "; Global_references (TRUE); echo "VAR2 is set to ' $var 2 ' "; ?> |
The results of the output are as follows:
VAR2 is set to ' &&&&& '
VAR2 is set to ' ##### '
Visible, in the code above:
$var 2 =& $var 1; 1
Visible only within the function.
and
$GLOBALS ["Var2"] =& $var 1; 2
Visible in the global scope.
http://www.bkjia.com/PHPjc/632616.html www.bkjia.com true http://www.bkjia.com/PHPjc/632616.html techarticle the variables defined by global are global variables, so to speak, if a variable in a function is not a variable in PHP with the same name as the outside of the function, if we use global to define in the function ...