Home > php教程 > php手册 > body text

php中global和$GLOBALS[]用法区别

WBOY
Release: 2016-06-13 10:15:14
Original
1175 people have browsed it

php中global和$GLOBALS[]是比较常见全局变量了,下面我们给大家看一个简单的实例,来区别一下global和$GLOBALS[]用法吧。

根据官方的解释是 :

1.$GLOBALS['var']是外部的全局变量本身。

 代码如下 复制代码

$var1 = 1; 
function test(){ 
     unset($GLOBALS['var1']); 

test(); 
echo $var1; 
?> 


2.global $var是外部$var的同名引用或者指针。

 

 代码如下 复制代码
$globalStr = '.com';
 
function globalTest(){
 
 global $globalStr;
 
 $globalStr = 'coderbolg'.$globalStr;
 
 unset($globalStr);
 
}
 
globalTest();
 
echo $globalStr; //输入: coderbolg.com
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template