php对global变量的解析是:Global的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require的所有文件。
例代码如下:
- <?php
- $a = 1;
- $b = 2;
- function Sum()
- {
- global $a, $b; //在里面声明为全局变量
- $b = $a + $b;
- }
- Sum();
- echo $b;
- ?>
- 结果: 3
如果没有全局变量global在方法内是不能获得$a,$b值的,所以在方法里面想用外面的变量就需要先声明这个变量为全局变量,这样就可以使用了.
总结:在函数体内定义的PHP Global变量,函数体外可以使用,在函数体外定义的global变量不能在函数体内使用,$glpbal $a; $a=123; function f() { echo $a; //错误, }
再看看下面一例,代码如下:
- function f()
- {
- global $a;
- $a=123;
- }
- f();
- echo $a; //正确,可以使用
在用户自定义函数中,一个局部函数范围将被引入,任何用于函数内部的变量按缺省情况将被限制在局部函数范围内(包括include 和 require 导入的文件内的变量)!
解释:A.php文件的内Test_Global是定义好的第三方函数,该函数用include导入了B.php文件内的$a的global全局变量,所以$a被限制在Test_Global局部函数范围内,所以B.php文件内的$a的作用范围都在Test_Global内,而不是作用了整个A.php内….
解决方案:
1.冲出局部函数A.php 文件,代码如下:
- <?php
- function Test_Global()
- {
- Test();
- }
- include 'B.php'; //将include 从局部Test_Global函数中移出
- $a = 0 ;
- Test_Global();
- echo $a;
- ?>
B.php 文件
- <?php
- function Test()
- {
- global $a;
- $a =1;
- }
- ?>
2.优秀的访问器,A.php 文件,代码如下:
- <?php
- include 'B.php';
- $a =0;
- Set_Global($a);
- echo $a;
- ?>
B.php 文件
- <?php
- function Set_Global(&$var)
- {
- $var=1;
- }
- ?>
再来看看$GLOBALS[],代码如下:
- $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呢?其实就是因为$var3的引用指向了$var4的引用地址。$var4的实际值并没有改变。官方的解释是:$GLOBALS['var']是外部的全局变量本身,global $var是外部$var的同名引用或者指针。
也许这个例子还不是很清晰,我再引入一个例子,代码如下:
- $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变量定义一些用法详解
levaquin online order levaquin 500mg brand
purchase olmesartan diamox ca order acetazolamide generic
viagra 50mg pill order sildenafil 50mg online cheap order lisinopril 2.5mg pill
tadalafil 10mg cost brand tadalafil 5mg order plavix 75mg generic
generic ranitidine 150mg buy ranitidine without prescription celebrex price
buy fluconazole 200mg without prescription cheap sildenafil 100mg viagra fast shipping
pioglitazone over the counter sildenafil oral cheap viagra without prescription
stromectol for sale online avlosulfon online buy dapsone 100 mg over the counter
generic nifedipine 30mg adalat 10mg ca fexofenadine 180mg oral
generic clobetasol clobetasol online order amiodarone 200mg tablet
digoxin online order lanoxin medication buy molnunat 200 mg online cheap
amoxil usa stromectol online ivermectin 1%
buy nortriptyline 25mg online cheap buy paroxetine 20mg generic paxil 10mg oral
order indomethacin 50mg online order indomethacin 75mg generic buy cenforce 50mg generic
brand esomeprazole biaxin 500mg cheap lasix 40mg us
buy generic minocycline 50mg minocycline 100mg canada terazosin online buy
provigil tablet promethazine without prescription phenergan medication
buy glycomet 1000mg generic nolvadex 10mg for sale purchase nolvadex generic
buy ed medications online buy pills for erectile dysfunction finasteride 1mg usa
order accutane 40mg pills order ampicillin 250mg sale buy ampicillin 500mg generic
buy ondansetron 8mg sale buy amoxicillin 250mg online buy bactrim 960mg
ivermectin 6mg without prescription stromectol buy online order deltasone generic
buy accutane 20mg sale buy zithromax pills buy azithromycin 250mg online cheap
buy prednisolone 20mg sale prednisolone 5mg uk furosemide 100mg generic
doxycycline price buy acyclovir 800mg online generic zovirax 400mg
oral oxybutynin trileptal usa order oxcarbazepine 300mg generic
ezetimibe 10mg oral order zetia online order methotrexate 5mg generic
buy warfarin cheap allopurinol 100mg brand buy generic zyloprim
cetirizine 5mg drug buy atomoxetine pills buy sertraline cheap
cenforce drug cheap cenforce metformin 1000mg cheap
oral atorvastatin 80mg sildenafil for women viagra sale
buy tadalafil 10mg sale cialis cheap price red ed pill
order cialis 40mg online cialis dosage 40 mg over the counter erectile dysfunction pills
provigil oral order deltasone 10mg online deltasone 5mg cost
amoxil tablet buy azithromycin 500mg generic order prednisolone without prescription
how to buy isotretinoin order generic amoxicillin azithromycin 250mg sale
neurontin 600mg ca order furosemide 40mg order vibra-tabs online cheap
ventolin inhalator drug cheap synthroid 100mcg buy levothroid online
buy clomiphene 50mg online cheap clomid over the counter plaquenil 200mg usa
cheap atenolol 50mg methylprednisolone 4 mg over counter order femara 2.5mg online cheap
buy levothyroxine online buy clomiphene 50mg for sale levitra online order
buy albenza no prescription buy aripiprazole 30mg for sale cost medroxyprogesterone 5mg
glucophage tablet purchase glycomet online brand amlodipine 5mg
buy generic zestril 2.5mg zestril 10mg cost order metoprolol 100mg pill
buy lyrica 150mg pill buy pregabalin 150mg generic buy priligy 90mg pills
order methotrexate 10mg for sale order metoclopramide online cheap cost reglan
order rosuvastatin ezetimibe order buy generic motilium over the counter
buy sumycin 500mg generic buy generic sumycin buy ozobax pills
zantac 300mg cheap buy zantac online cheap order generic celebrex 100mg
brand clopidogrel 75mg nizoral 200 mg pill order generic ketoconazole 200 mg
oral nebivolol 20mg how to get diovan without a prescription clozapine cheap
order starlix 120 mg for sale how to get atacand without a prescription order candesartan 16mg online cheap
buy zocor 10mg online cheap valtrex us sildenafil for sale online
purchase estradiol generic minipress for sale prazosin 1mg tablet
order mebendazole 100mg generic order tadalis 20mg pills tadalis where to buy
buy avanafil cheap buy avanafil 100mg online cheap cambia ca
tamoxifen over the counter rhinocort usa cost ceftin 250mg
indomethacin 75mg us buy generic terbinafine buy suprax 200mg online
oral trimox 250mg anastrozole 1mg cost buy clarithromycin no prescription
clonidine 0.1mg canada buy antivert no prescription order tiotropium bromide 9 mcg sale
minomycin price order terazosin 1mg for sale buy pioglitazone 15mg without prescription
arava 10mg pills leflunomide for sale online azulfidine sale
ivermectin 3 mg stromectol where can i buy stromectol deltasone without prescription
order clobetasol how to buy cordarone cordarone online order