$GLOBALS['var']是外部的全局变量本身,global $var是外部$var的同名引用或者指针
一、举例比较
例一:
复制代码 代码如下:
$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其实就是&$GLOBALS['var'],调用外部变量的一个别名而已。
上面代码中的$var1和$GLOBALS['var1']是指的同一变量,而不是两个不同的变量。
PHP的全局变量和C语言有一点点不同。在C语言中,全局变量在函数中主动生效,除非被局部变量笼罩。这可能引起一些问题,有些人可能漫不经心的转变一个全局变量。PHP中全局变量在函数中应用时必须用global申明为全局。
PHP的Global变量的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require的所有文件。
三、结论
1.$GLOBALS['var']是外部的全局变量本身
2.global $var是外部$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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
