日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP中全局变量的使用global和$GLOBALS[]

發布時間:2023/12/9 php 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP中全局变量的使用global和$GLOBALS[] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

From:?http://blog.csdn.net/happyqyt/article/details/7219889

用PHP開發項目,不可避免的會使用到全局變量,比如一些網站的配置信息,全站通用,那就可以在一個地方設置,然后多個地方調用。

把變量定義為全局變量可以有兩種方法:global和$GLOBALS[]。很多人都認為global和$GLOBALS[]只是寫法不同而已,其實是有差別的。

先來看看global
php對global變量的解析是:global的作用是定義全局變量,但是這個全局變量不是應用于整個網站,而是應用于當前頁面,包括include或require的所有文件。

看一下下面一段PHP代碼:

[php] view plaincopyprint?
  • $a=123;??
  • function?test1(){??
  • ????global?$a;??//如果不把$a定義為global變量,函數體內是不能訪問$a的 ??
  • ????echo?$a;??//123 ??
  • }??
  • test1();??
  • ??
  • global?$b;??
  • $b?=?456;??
  • function?test2(){??
  • ????var_dump($b);??//NULL ??
  • }??
  • test2();??
  • ??
  • function?test3(){??
  • ????global?$c;??
  • ????$c=789;??
  • }??
  • test3();??
  • echo?$c;??//789??
  • $a=123; function test1(){global $a; //如果不把$a定義為global變量,函數體內是不能訪問$a的echo $a; //123 } test1();global $b; $b = 456; function test2(){var_dump($b); //NULL } test2();function test3(){global $c;$c=789; } test3(); echo $c; //789

    [php] view plaincopyprint?
  • $a=123;??
  • function?test1()??
  • {??
  • global?$a;???//如果不把$a定義為global變量,函數體內是不能訪問$a的 ??
  • echo?$a;?//123 ??
  • }??
  • test1();??
  • ??
  • global?$b;??
  • $b=456;??
  • function?test2()??
  • {??
  • var_dump($b);????//NULL ??
  • }??
  • test2();??
  • ??
  • function?test3()??
  • {??
  • global?$c;??
  • $c=789;??
  • }??
  • test3();??
  • echo?$c;???//789??
  • $a=123; function test1() { global $a; //如果不把$a定義為global變量,函數體內是不能訪問$a的 echo $a; //123 } test1();global $b; $b=456; function test2() { var_dump($b); //NULL } test2();function test3() { global $c; $c=789; } test3(); echo $c; //789 通過代碼得出總結:在函數體內定義的global變量,函數體外可以使用,在函數體外定義的global變量不能在函數體內使用。

    再來看看$GLOBALS[]

    [php] view plaincopyprint?
  • $var1=1;??
  • $var2=2;??
  • function?test1(){??
  • ????$GLOBALS['var2']=&$GLOBALS['var1'];??
  • }??
  • test1();??
  • echo?$var2;??//1 ??
  • ??
  • $var3=1;??
  • $var4=2;??
  • function?test2(){??
  • ????global?$var3,$var4;??
  • ????$var4=&$var3;??
  • }??
  • test2();??
  • echo?$var4;??//2??
  • $var1=1; $var2=2; function test1(){$GLOBALS['var2']=&$GLOBALS['var1']; } test1(); echo $var2; //1$var3=1; $var4=2; function test2(){global $var3,$var4;$var4=&$var3; } test2(); echo $var4; //2

    [php] view plaincopyprint?
  • $var1?=?1;????
  • $var2?=?2;????
  • function?test1(){????
  • ?????$GLOBALS['var2']?=?&$GLOBALS['var1'];????
  • }????
  • test1();????
  • echo?$var2;??//1 ??
  • ??
  • $var3?=?1;????
  • $var4?=?2;????
  • function?test2(){????
  • ?????global?$var3,$var4;????
  • ?????$var4?=?&$var3;????
  • }????
  • test2();????
  • echo?$var4;???//2??
  • $var1 = 1; $var2 = 2; function test1(){ $GLOBALS['var2'] = &$GLOBALS['var1']; } test1(); echo $var2; //1$var3 = 1; $var4 = 2; function test2(){ global $var3,$var4; $var4 = &$var3; } test2(); echo $var4; //2 為什么$var2的打印結果是1,而$var4的打印結果為2呢?其實就是因為$var4的引用指向了$var3的引用地址。$var4的實際值并沒有改變。官方的解釋是:$GLOBALS['var']是外部的全局變量本身,global $var是外部$var的同名引用或者指針。

    也許這個例子還不是很清晰,我再引入一個例子:

    [php] view plaincopyprint?
  • $var1?=?1;??????
  • function?test1(){??????
  • ?????unset($GLOBALS['var1']);??????
  • }??????
  • test1();??????
  • var_dump($var1);???//NULL?? ??
  • ??
  • $var2?=?1;??????
  • function?test2(){??????
  • ????global??$var2;??????
  • ?????unset($var2);??????
  • }??????
  • test2();??????
  • echo?$var2;???//1???
  • $var1 = 1; function test1(){ unset($GLOBALS['var1']); } test1(); var_dump($var1); //NULL $var2 = 1; function test2(){ global $var2; unset($var2); } test2(); echo $var2; //1 [php] view plaincopyprint?
  • $var1?=?1;????
  • function?test1(){????
  • ?????unset($GLOBALS['var1']);????
  • }????
  • test1();????
  • var_dump($var1);???//NULL ??
  • ??
  • $var2?=?1;????
  • function?test2(){????
  • ????global??$var2;????
  • ?????unset($var2);????
  • }????
  • test2();????
  • echo?$var2;???//1??
  • $var1 = 1; function test1(){ unset($GLOBALS['var1']); } test1(); var_dump($var1); //NULL$var2 = 1; function test2(){ global $var2; unset($var2); } test2(); echo $var2; //1

    $var1的值被刪除,而$var2的值還存在。這就證明,$var2只是別名引用,本身的值沒有受到任何的改變。

    也就是說global $var其實就是$var = &$GLOBALS['var'],調用外部變量的一個別名而已!

    總結

    以上是生活随笔為你收集整理的PHP中全局变量的使用global和$GLOBALS[]的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。