Variables are divided into global variables and local variables. Children who have learned the C language know that the scope of the global variable is the entire file. It works even inside the function, but in PHP, if you use a global variable in a function, PHP will assume that the variable is undefined. If we need to use this global variable inside the function, then we need to add the keyword Global to the inside of the function, before the global variable. Here is a small demo of your own. To prove what I said.
$str = "string"; functiontest() {if (isset($str)) { echo"the string is defined"; } else { echo"the string is undefined"; } } test();?>
This is the result of running in the browser:
The string is undefined
$str = "string"; functiontest() {global$str;//上面的test函数中没有这句话if (isset($str)) { echo"the string is defined"; } else { echo"the string is undefined"; } } test();?>
This is the result of running in the browser:
The string is defined
$GLOBALS-Referencing all variables available in the global scope
A globally combined array that contains all the variables. The name of the variable is the key of the array.
functiontest() {$foo = "local variable"; echo'$foo in global scope: ' . $GLOBALS["foo"] . "\n"; echo'$foo in current scope: ' . $foo . "\n";}$foo = "Example content";test();?>
The output of the above routines is similar to the following:
$fooinglobal scope: Example content$fooinlocalvariable
'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
'). Text (i)); }; $numbering. FadeIn (1700); }); });
The above describes the use of global in PHP, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.