The
global keyword is used to access global variables inside a function.
Sample code
<?php$x = 5; $y = 10; function myTest(){ global $x,$y; $x = $x+$y; } myTest(); echo $x; //15
php stores all global variables in a array named $GLOBALS[index]. index saves the name of the variable. This array can be For internal access within the function, you can also directlyupdateglobal 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
The above is the detailed content of Introduction to the usage of php global keyword. For more information, please follow other related articles on the PHP Chinese website!