global and $GLOBALS[] are relatively common global variables in PHP. Let’s show you a simple example to distinguish the usage of global and $GLOBALS[].
According to the official explanation:
1.$GLOBALS['var'] is the external global variable itself.
The code is as follows
代码如下 |
复制代码 |
$var1 = 1;
function test(){
unset($GLOBALS['var1']);
}
test();
echo $var1;
?>
|
|
Copy code
|
$var1 = 1;
function test(){
代码如下 |
复制代码 |
$globalStr = '.com';
function globalTest(){
global $globalStr;
$globalStr = 'coderbolg'.$globalStr;
unset($globalStr);
}
globalTest();
echo $globalStr; //输入: coderbolg.com
|
unset($GLOBALS['var1']); |
}
test();
echo $var1;
?>
2.global $var is a reference or pointer of the same name as an external $var.
The code is as follows
|
Copy code
|
$globalStr = '.com';
function globalTest(){
global $globalStr;
unset($globalStr);
}
globalTest();
echo $globalStr; //Input: coderbolg.com
http://www.bkjia.com/PHPjc/629047.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629047.htmlTechArticleglobal and $GLOBALS[] are relatively common global variables in php. Let’s show you a simple example below. , let’s differentiate between the usage of global and $GLOBALS[]. According to the official explanation: 1.$...
|