深入理解php中$GLOBALS与global区别
在php中$GLOBALS是超全局变量而global也是一个全局变量,那么它们在应用中有什么区别呢,下面我就整理了一些相关的例子各各位同学一起分享吧。
php中有一个不为很多人所用的超全局变量$GLOBALS。合理使用这个变量能使工作变得更加有效率,本篇文章主要分析一下这个超全局变量的用法以及$GLOBALS和global的区别。
$GLOBALS定义:引用全局作用域中可用的全部变量(一个包含了全部变量的全局组合数组。变量的名字就是数组的键),与所有其他超全局变量不同,$GLOBALS在PHP代码中任何地方总是可用的,自己可以通过打印$GLOBALS这个变量的结果就知道了。
global与$GLOBALS['']区别
$GLOBALS
$GLOBALS — 引用全局作用域中可用的全部变量
说明 ¶
一个包含了全部变量的全局组合数组。变量的名字就是数组的键。
在PHP生命周期中,定义在函数体外部的所谓全局变量,函数内部是不能直接获得的。如果要在函数体内访问外部定义的全局变量,可以通过global声明或者直接使用$GLOBALS来进行访问,例如:
$var1='www.111cn.net';
$var2='www.google.cn';
test();
function test(){
$var1='taobao';
echo $var1,'
';
global $var1;
echo $var1,'
';
echo $GLOBALS['var2'];
}
结果将打印为:
taobao
www.111cn.net
www.google.cn
下面主要说明一下global和$GLOBALS的区别:
$GLOBALS['var']是外部的全局变量本身,而global $var是外部$var的同名引用或者指针,也就是说global在函数产生一个指向函数外部变量的别名变量,而不是真正的函数外部变量,而$GLOBALS[]确确实实调用的是外部的变量,函数内外会始终保持一致。
下面通过例子说明一下:
$var1=1;
$var2=2;
function test(){
$GLOBALS['var2']=&$GLOBALS['var1'];
}
test();
echo $var2;
打印结果为1
$var1=1;
$var2=2;
function test(){
global $var1,$var2;
$var2=&$var1;
}
test();
echo $var2;
打印结果为2。为什么会打印结果为2呢?其实就是因为$var1的引用指向了$var2的引用地址。导致实质的值没有改变。我们再来看一个例子吧。
$var1=1;
function test(){
unset($GLOBALS['var1']);
}
test();
echo $var1;
因为$var1被删除了,所以什么东西都没有打印。
$var1=1;
function test(){
global $var1;
unset($var1);
}
test();
echo $var1;
打印结果为1。证明删除的只是别名|引用,起本身的值没有受到任何的改变。也就是说global $var其实就是$var=&$GLOBALS['var']。调用外部变量的一个别名而已。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Huawei is rolling out software version 5.0.0.100(C00M01) for the Watch GT 5 and the Watch GT 5 Prosmartwatchesglobally. These two smartwatches recently launched in Europe, with the standard model arriving as the company’s cheapest model. This Harmony

Katsuhiro Harada, the Tekken series director, once seriously tried to bring Colonel Sanders into the iconic fighting game. In an interview with TheGamer, Harada revealed that he pitched the idea to KFC Japan, hoping to add the fast-food legend as a g

Tesla is rolling out the latest Full Self-Driving (Supervised) version 12.5.5 and with it comes the promised Cybertruck FSD option at long last, ten months after the pickup went on sale with the feature included in the Foundation Series trim price. F

Garmin is ending the month with a new set of stable updates for its latest high-end smartwatches. To recap, the company released System Software 11.64 to combat high battery drain across the Enduro 3, Fenix E and Fenix 8 (curr. $1,099.99 on Amazon).

Xiaomi will shortly launch the Mijia Graphene Oil Heater in China. The company recently ran a successful crowdfunding campaign for the smart home product, hosted on its Youpin platform. According to the page, the device has already started to ship to

Earlier in September 2024, Anker's Zolo 140W charger was leaked, and it was a big deal since it was the first-ever wall charger with a display from the company. Now, a new unboxing video from Xiao Li TV on YouTube gives us a first-hand look at the hi

The launch of Samsung's long-awaited 'Special Edition' foldable has taken another twist. In recent weeks, rumours about the so-called Galaxy Z Fold Special Edition went rather quiet. Instead, the focus has shifted to the Galaxy S25 series, including

With a history of over one decade, Manjaro is regarded as one of the most user-friendly Linux distros suitable for both beginners and power users, being easy to install and use. Mostly developed in Austria, Germany, and France, this Arch-based distro
