Home Backend Development PHP Tutorial PHP中全局变量的应用global和$GLOBALS[]

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

Jun 13, 2016 pm 01:13 PM
function global var

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

用PHP开发项目,不可避免的会使用到全局变量,比如一些网站的配置信息,全站通用,那就可以在一个地方设置,然后多个地方调用。把变量定义为全局变量可以有两种方法:global和$GLOBALS[]。很多人都认为global和$GLOBALS[]只是写法不同而已,其实是有差别的。

先来看看global
php对global变量的解析是:Global的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require的所有文件。

看一下下面一段PHP代码:

$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
Copy after login

?通过代码得出总结:在函数体内定义的global变量,函数体外可以使用,在函数体外定义的global变量不能在函数体内使用。

再来看看$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
Copy after login

?为什么$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
Copy after login

?$var1的值被删除,而$var2的值还存在。这就证明,$var2只是别名引用,本身的值没有受到任何的改变。也就是说global $var其实就是$var = &$GLOBALS['var'],调用外部变量的一个别名而已!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does function mean? What does function mean? Aug 04, 2023 am 10:33 AM

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

The role and examples of var keyword in PHP The role and examples of var keyword in PHP Jun 28, 2023 pm 08:58 PM

The role and examples of var keyword in PHP In PHP, the var keyword is used to declare a variable. In previous PHP versions, using the var keyword was the idiomatic way to declare member variables, but its use is no longer recommended. However, in some cases, the var keyword is still used. The var keyword is mainly used to declare a local variable, and the variable will automatically be marked as local scope. This means that the variable is only visible within the current block of code and cannot be accessed in other functions or blocks of code. Use var

Detailed explanation of the role and function of the MySQL.proc table Detailed explanation of the role and function of the MySQL.proc table Mar 16, 2024 am 09:03 AM

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

What is the purpose of the 'enumerate()' function in Python? What is the purpose of the 'enumerate()' function in Python? Sep 01, 2023 am 11:29 AM

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Let's talk about the differences between var, let and const (code example) Let's talk about the differences between var, let and const (code example) Jan 06, 2023 pm 04:25 PM

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

The usage and function of Vue.use function The usage and function of Vue.use function Jul 24, 2023 pm 06:09 PM

Usage and Function of Vue.use Function Vue is a popular front-end framework that provides many useful features and functions. One of them is the Vue.use function, which allows us to use plugins in Vue applications. This article will introduce the usage and function of the Vue.use function and provide some code examples. The basic usage of the Vue.use function is very simple, just call it before Vue is instantiated, passing in the plugin you want to use as a parameter. Here is a simple example: //Introduce and use the plug-in

file_exists() function in PHP file_exists() function in PHP Sep 14, 2023 am 08:29 AM

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for - it's useful when you need to know if a file exists before processing it. This way, when creating a new file, you can use this function to know if the file already exists. Syntax file_exists($file_path) Parameters file_path - Set the path of the file or directory to be checked for existence. Required. Return file_exists() method returns. Returns TrueFalse if the file or directory exists, if the file or directory does not exist Example let us see a check for "candidate.txt" file and even if the file

See all articles