PHP defines some super global variables by default. these variables are automatically normalized and can be called anywhere in the program, such as $ _ GET and $ _ REQUEST.
PHP defines some "Superglobals" variables by default. these variables are automatically normalized and can be called anywhere in the program, such as $ _ GET and $ _ REQUEST, they generally come from data or other external data. using these variables will not cause problems, because they are basically not writable.
However, you can use your own global variables. With the keyword "global", you can import global data to a local range of a function. if you do not understand the "scope of use of variables ", please refer to the instructions in the PHP Manual.
The following is an example of using the "global" keyword:
- $ 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 seems that it works well and is very simple, so why do we need to worry about using the "global" keyword to define global data? I will explain the use of the global variable to the person at the entry, "global variable", the global word in this term tells us that this variable can be used everywhere. let's look at an example first:
- $ 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 $ a and $ B cannot be obtained in the method. therefore, to use an external variable in the method, you must declare this variable as a global variable first, this is convenient.
- $ W3sky = 1;
- $ W3sky2 = 2;
- Function Sum ()
- {
- Global $ w3sky, $ w3sky2; $ w3sky2 = $ w3sky + $ w3sky2;
- } Sum ();
- Echo $ w3sky2;
- ?>
The output of the above script is "3". The global variables $ w3sky and $ w3sky2 are declared in the function. all referenced variables of any variable point to the global variable, PHP has no limit on the maximum number of global variables that a function can declare.
The second way to access variables within the global scope is to use a special PHP custom $ GLOBALS array. the previous example can be written as: Replace global with $ GLOBALS.
- $ 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 content. $ GLOBALS exists globally because $ GLOBALS is a super global variable. The following example shows how to use a super global variable:
Examples of hyperglobal variables and scopes
- 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, you can reference this $ db under the declaration.
- Function SayMyABCs2 ()
- {
- Global $ count;
- While ($ count <10)
- {
- Print (chr (ord ('A') + $ count ));
- $ Count = $ count + 1;
- }
- Print ("
Now I know $ count letters
");
- }
- $ Count = 0;
- SayMyABCs2 ();
- $ Count = $ count + 1;
- Print ("Now I 've made $ count function call (s ).
");
- SayMyABCs2 ();
- $ Count = $ count + 1;
- Print ("Now I 've made $ count function call (s ).
");
- ?>
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 SayMyABCs2 () is called for the second time, $ count is already 11, so it does not enter the print letter loop at all. the scope of the variables defined in the function is limited to this function by default, and the global declaration is used, to notify PHP, you 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 page before the code is executed. it allows the function to see these variables, this saves the trouble of passing them into the function as parameters in each call.