The
global keyword is used to access global variables inside a function.
<?php $x = 5; $y = 10; function myTest(){ global $x,$y; $t = $x+$y; } myTest(); echo $t; //15
php stores all global variables in an array named $GLOBALS[index]. index saves the name of the variable. This array can be accessed inside the function or directly updated global variables;
The above example can be rewritten as follows:
<?php $x = 5; $y=10; function myTest(){ $GLOBALS['x'] = $GLOBALS['x'] + $GLOBALS['y']; } myTest(); echo $x; //15
<span> </span>
The above introduces the PHP global keyword, including global variables. I hope it will be helpful to friends who are interested in PHP tutorials.