The role of the PHP keyword global in defining variables. If we use the PHP keyword global in the function, it means that the variable used in the function is global, and the global variable can work throughout the page. For example, we are using
If the PHP keyword global is used inside the function, it indicates that the variable used in the function is global, and the global variable is available throughout the page. For example
- $ Conf = 1;
- Function conf_test (){
- Global $ conf;
- Return ++ $ conf;
- }
- Echo conf_test ()."< Br>";
- Echo conf_test ()."< Br>";
Output:
2
3
If there is no global $ conf;, the output will be 1. The global keyword of PHP is used to declare that $ conf in the function is not local, but global. Or, the $ conf defined in the function is not a variable in the function, but the $ conf defined outside the function (that is, $ conf = 1. in fact, if $ GLOBALS array is used here, it is easier to understand.
We declare a variable $ conf on the page, which is equivalent to defining a $ GLOBALS ['conf'] in the $ GLOBALS array. this $ GLOBALS is globally visible. Therefore, the above code is written in the $ GLOBALS format.
- $conf = 1;
- function conf_test() {
- return ++$GLOBALS['conf'];
- }
- echo conf_test()."
";
- echo conf_test()."
";
Output:
2
3
PHP keyword global
If the keyword "global" is used inside the function, it indicates that the variable used in the function is global, and the global variable is available throughout the page. For example...