This article describes the use and precautions of PHP global global variables. Share it for your reference, as follows:
Use global to declare the external variable as a global variable in the method, that is, the variable can be called.
Example 1. Basic usage of global
<?php $a=1; $b=2; test_global(); function test_global() { global $a,$b; echo $a+$b; //Output 3}
Example 2. Global Reference
<?php $var $a=1; function test(){ global $a; $a++; unset($a); } test(); echo $a;
Running results:
2
Notice:
1. The global in the function here refers to the external $a, which is a reference or pointer of the same name as the external variable $a. Therefore, $a++ inside the function can change the value of the external $a.
2. unset($a) is a deleted alias reference and has no effect on the value of the variable.
Supplement: $GLOBALS['var'] global variable application
<?php $a = 1; function test(){ unset($GLOBALS['a']); } test(); echo $a;
Run the above code and the output is empty!
Notice:Here $GLOBALS['a'] is the external variable itself! That is: global $a is equivalent to &$GLOBALS['a'].
For more information about PHP related content, please check out the topic of this site:Summary of common functions and techniques for php》、《Summary of usage of php strings》、《PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm"and"Complete collection of PHP array (Array) operation techniques》
I hope this article will be helpful to everyone's PHP programming.