Blogger Information
Blog 9
fans 0
comment 0
visits 7684
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
$GLOBALS与global区别
你微笑時好美丶的博客
Original
1006 people have browsed it

例子1:

<?php
$var1 = 1;
function test1(){
    
global $var1;  #等价于 $var1 = &$GLOBALS['var1']; 这里的$var1跟外面的$var1是不同的指针,但指向同样的数据
unset($var1);   #当你unset一个引用,只是断开了变量名和变量内容之间的绑定,这并不意味着变量内容被销毁了.
 echo $var1;     #PHP Notice:  
Undefined variable: var1}
test1();
echo $var1;    #此处输出1

例子2:

<?php
$var1 = 1;
function test1(){
global $var1;    
unset($GLOBALS['var1']);    
echo $var1;  #输出1
}
test1();echo $var1;

总结:

1、$GLOBALS['var1'] 和外部的 $var1 是同一个指针,指向存储值为2的内存地址

2、global $var1 是外部的 $val1 的指针的一个复制指针,也指向存储值为1的内存地址(同引用符号&)

3、无论$GLOBALS['var1']或者global $var1,因为指向的值内存地址一样,所以都可以修改外部变量的值。


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post