Home > Backend Development > PHP Tutorial > The difference in usage between global and $GLOBALS[] in php_PHP tutorial

The difference in usage between global and $GLOBALS[] in php_PHP tutorial

WBOY
Release: 2016-07-13 17:14:19
Original
872 people have browsed it

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.

} test();
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']);  
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;
$globalStr = 'coderbolg'.$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.$...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template