Global variables and "global" keywords
PHP defines some "Superglobals" variables by default. These variables are automatically merged and can be
Programs are called anywhere, such as $ _ GET and $ _ REQUEST. They generally come from data or other external numbers.
Normally, using these variables will not cause problems, because they are basically not writable.
However, you can use your own global variables. You can import global data to
Within the local range of the function. If you do not understand the scope of use of variables, please refer to the relevant instructions in the PHP Manual.
The following is an example of using the "global" keyword:
<? Php Tutorial
$ My_var = 'Hello World ';
Test_global ();
Function test_global (){
// Now in local scope
// The $ my_var variable doesn't exist
// Produces error: "Undefined variable: my_var"
Echo $ my_var;
// Now let's important the variable
Global $ my_var;
// Works:
Echo $ my_var;
}
?>
As you can see in the preceding example, the "global" keyword is used to import global variables. It looks like it works
It is very easy, so why do we need to worry about using the "global" keyword to define global data?
Let me explain the use of global as the global variable to the person at the entry.
This variable can be used everywhere. Let's look at an instance first:
<? Php
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B; // declare it as a global variable
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>
Result: 3
If no global variable is available, the values of $ a and $ B cannot be obtained in the method. Therefore, if you want to use an external variable in the method
You need to declare this variable as a global variable so that it can be used.
<? PHP
$ W3sky = 1;
$ W3sky2 = 2;
Function Sum ()
{
Global $ w3sky, $ w3sky2; $ w3sky2 = $ w3sky + $ w3sky2;
} Sum ();
Echo $ w3sky2;
?>
The output of the above script is "3 ". Specify the global variables $ w3sky and $ w3sky2 in the function.
All referenced variables point to global variables. PHP is not limited to the maximum number of global variables that a function can declare
.
The second way to access variables globally is to use a special PHP custom $ GLOBALS array. Previous example
Can be written:
Replace global with $ GLOBALS.
<? PHP
$ W3sky = 1;
$ W3sky2 = 2; function Sum ()
{
$ GLOBALS ['w3sky'] = $ GLOBALS ['w3sky'] + $ GLOBALS ['w3sky2 '];
} Sum ();
Echo $ w3sky2;
?>
In the $ GLOBALS array, each variable is an element. The key name corresponds to the variable name, and the value corresponds to the variable
. $ GLOBALS exists globally because $ GLOBALS is a super global variable. The following example shows
Demonstrate the usage of Super global variables:
Examples: examples of hyperglobal variables and scopes
<? PHP
Function test_global ()
{
// Most of the predefined variables are not "super". They need to use the 'global' keyword to make them
Number of local regions.
Global $ HTTP_POST_VARS; echo $ HTTP_POST_VARS ['name']; // Superglobals in any
They are valid within the specified range and do not require 'global' declarations. Superglobals was introduced in PHP 4.1.0.
Echo $ _ POST ['name'];
}
?> Global means that if you declare global $ db in a file, then you will
You can reference this $ db.
<? Php
Function SayMyABCs2 ()
{
Global $ count;
While ($ count <10)
{
Print (chr (ord ('A') + $ count ));
$ Count = $ count + 1;
}
Print ("<br> Now I know $ count letters <br> ");
}
$ Count = 0;
SayMyABCs2 ();
$ Count = $ count + 1;
Print ("Now I 've made $ count function call (s). <br> ");
SayMyABCs2 ();
$ Count = $ count + 1;
Print ("Now I 've made $ count function call (s). <br> ");
?>
Output:
ABCDEFGHIJ // print 10 uppercase letters in sequence in the first cycle
Now I know 10 letters
Now I 've made 11 function call (s). // For the second time, no loop is executed because the condition is greater than 10.
Now I know 11 letters
Now I 've made 12 function call (s ).
Due to the global declaration of variables, there is only one $ count variable, which is added both inside and outside the function. When
When SayMyABCs2 () is called for the second time, $ count is already 11, so it does not enter the print letter loop. In the letter
The scope of the variable defined in the number is limited to this function by default. You can use the global declaration to notify PHP,
Now we need to make a variable name have the same meaning in the function environment. Statement format: global
$ Count1, $ count2,..., $ countn;
Global is useful on the other hand, especially because PHP provides some variables that can be bound to each
On the page, it allows the function to see these variables, eliminating the need to pass them into the function as parameters in each call.
Trouble