PHP Global与$GLOBALS变量作用域与区别
在php中变量有很多种如普通的变量及全局变量(Global与$GLOBALS),本文章来给大家介绍在php中Global与$GLOBALS的用法区别.
Global,全局变量
PHP Global变量在实际应用中会发现许多问题需要我们不断的去完善处理.我们在这篇文章中就针对PHP Global变量出现的问题给出了一些具体的解决办法,PHP hack的使用技巧详解,代码实现PHP GTK写文本查看器,网站开发中PHP语言优缺点.
如何正确实现PHP function函数扩展
PHP error_log()函数处理错误日志
1:PHP Global变量的作用是定义全局变量,但是这个全局变量不是应用于整个网站,而是应用于当前页面,包括include或require的所有文件
实例代码如下:
$a=123; function aa() { Global $a; //如果不把$a定义为global变量,函数体内是不能访问$a的 echo $a; } aa();
总结:
在函数体内定义的PHP Global变量,函数体外可以使用,在函数体外定义的global变量不能在函数体内使用,
实例代码如下:
$glpbal $a; $a=123; function f() { echo $a; //错误, }
再看看下面一例,实例代码如下:
function f() { global $a; $a=123; } f(); echo $a; //正确,可以使用
2:PHP Global变量问题解析:
question:我在config.inc.php中定义了一些变量($a),在别的文件中函数外部 include("config.inc.php"),函数内部需要使用这些变量$a,如果没有声明的话,echo $a是打印不出来任何东西的.因此声明global $a,但是有很多函数和很多变量.总不能不断重复的这样声明吧?有什么好的解决办法,请指点.
answer1:先在config.inc.php里定义常量:define(常量名,常量值),再在其他需要用到的地方require 'config.inc.php',然后就能在这个文件里直接使用这个常量了.
answer2:我也有个办法,就是定义数组,如$x[a],$x,那样就只要声明global $x一个了.
answer3:我试了你的这个方法,不行啊.
answer4:改你的php.ini文件.
设置PHP Global变量 为 on,下面我们看看复杂点的:
实例代码如下:
//A.php 文件 <?php function Test_Global() { include 'B.php'; Test(); } $a = 0; Test_Global(); echo $a; ?>
//B.php 文件 <?php function Test() { global $a; //申明函数体Sum内使用的$a变量为global全局变量 $a = 1; } ?>
为什么输出的却是0?!!
在用户自定义函数中,一个局部函数范围将被引入.任何用于函数内部的变量按缺省情况将被限制在局部函数范围内(包括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; } ?>
global和$GLOBALS的区别
php中global和$GLOBALS不仅仅是写法不一样以为,2者的区别还是很大的,在实际应用中需要注意!
先看下面的实例代码:
<?php // 例子1 function test_global() { global $var1, $var2; $var2 = & $var1; } function test_globals() { $GLOBALS['var3'] = & $GLOBALS['var1']; } $var1 = 5; $var2 = $var3 = 0; test_global(); print $var2 . "\n"; test_globals(); print $var3 . "\n"; ?>
执行结果为:0 5
怎么会这样呢?不应该是2个5吗?怎么会出现1个0和1个5呢?
恩,我们保留以上问题,深入分析$GLOBALS和global的原理!我们都知道变量其实是相应物理内存在代码中的"代号"而已引用php手册的$GLOBALS的解释:
Global 变量:$GLOBALS,注意: $GLOBALS 在 PHP 3.0.0 及以后版本中适用.由所有已定义全局变量组成的数组.变量名就是该数组的索引.这是一个"superglobal",或者可以描述为自动全局变量.也就是说上面代码中的$var1和$GLOBALS['var1']是指的同一变量,而不是2个不同的变量!下面来分析global到底做了什么?
引用php手册的global的解释:
如果在一个函数内部给一个声明为 global 的变量赋于一个引用,该引用只在函数内部可见.可以通过使用 $GLOBALS 数组避免这一点.我们都知道php中的函数所产生的变量都是函数的私有变量,那么global关键字产生的变量也肯定逃不出这个规则,为什么这么说呢,
请看下面的代码:
<?php // 例子2 function test() { global $a; unset($a); } $a = 1; test(); print $a; ?>
执行结果为:1
为什么会输出1呢?不是已经把$a给unset了吗?unset失灵了?php的bug?
都不是,其实unset起作用了,是把test函数中的$a给unset掉了,可以在函数test()中加入print $a;来测试!
接着回到上面的例子1,看test_global中的这一代码"$var2 = &$var1;",上面是一个引用赋值运算,也就是$var2将指向var1所指向的物理内存地址,所以例子1执行过test_global函数以后,变量的变化只在函数的局部产生效应,在函数外部$var2的指向物理内存地址并没有变化,还是它自己.此时,就能理解为什么例子1执行完以后,$var2是0,而$var3是5了!
所以我们得出一个结论,在函数中global和$GLOBALS[]的区别在于:
global在函数产生一个指向函数外部变量的别名变量,而不是真正的函数外部变量,一但改变了别名变量的指向地址,就会发生一些意料不到情况,例如例子1.
$GLOBALS[]确确实实调用是外部的变量,函数内外会始终保持一致,可以对照下面两个列子再加深下印象:
global:
实例代码如下:
<?php function myfunction() { global $bar; unset($bar); } $bar = "someting"; myfunction(); echo $bar; ?>
输出:someting
$GLOBALS[]:
<?php function foo() { unset($GLOBALS['bar']); } $bar = "something"; foo(); echo $bar; ?>
输出:空
当按照上面的思路理解后,碰到下面的情况是不是又有些晕呢?
实例代码如下:
<?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>
输出将是 "3".在函数中申明了全局变量 $a 和 $b,任何变量的所有引用变量都会指向到全局变量.
怎么不是2呢,在函数外部不是不影响吗,请注意$b在函数中并没有通过引用修改,而是修改的$b指向物理内存的值,因此外部输入为3.
php中global和$GLOBALS不仅仅是写法不一样以为,2者的区别还是很大的,在实际应用中需要注意!
先看下面的PHP代码例子:
<?php // 例子1 function test_global() { global $var1, $var2; $var2 = & $var1; } function test_globals() { $GLOBALS['var3'] = & $GLOBALS['var1']; } $var1 = 5; $var2 = $var3 = 0; test_global(); print $var2 . "\n"; test_globals(); print $var3 . "\n"; ?>
执行结果为:0 5
怎么会这样呢?不应该是2个5吗?怎么会出现1个0和1个5呢?
恩,我们保留以上问题,深入分析$GLOBALS和global的原理!我们都知道变量其实是相应物理内存在代码中的"代号"而已,引用php手册的$GLOBALS的解释:
Global 变量:$GLOBALS,注意: $GLOBALS 在 PHP 3.0.0 及以后版本中适用.
由所有已定义全局变量组成的数组.变量名就是该数组的索引.这是一个"superglobal",或者可以描述为自动全局变量.也就是说上面代码中的$var1和$GLOBALS['var1']是指的同一变量,而不是2个不同的变量!
下面来分析global到底做了什么?
引用php手册的global的解释:
如果在一个函数内部给一个声明为 global 的变量赋于一个引用,该引用只在函数内部可见.可以通过使用 $GLOBALS 数组避免这一点.我们都知道php中的函数所产生的变量都是函数的私有变量,那么global关键字产生的变量也肯定逃不出这个规则,为什么这么说呢,看下面的代码:
实例代码如下:
<?php // 例子2 function test() { global $a; unset($a); } $a = 1; test(); print $a; ?>
执行结果为:1
为什么会输出1呢?不是已经把$a给unset了吗?unset失灵了?php的bug?
都不是,其实unset起作用了,是把test函数中的$a给unset掉了,可以在函数test()中加入print $a;来测试!
接着回到上面的例子1,看test_global中的这一代码"$var2 =& $var1;",上面是一个引用赋值运算,也就是$var2将指向var1所指向的物理内存地址,所以例子1执行过test_global函数以后,变量的变化只在函数的局部产生效应,在函数外部$var2的指向物理内存地址并没有变化,还是它自己.
此时,就能理解为什么例子1执行完以后,$var2是0,而$var3是5了!
所以我们得出一个结论,在函数中global和$GLOBALS[]的区别在于:
global在函数产生一个指向函数外部变量的别名变量,而不是真正的函数外部变量,一但改变了别名变量的指向地址,就会发生一些意料不到情况,例如例子1.
$GLOBALS[]确确实实调用是外部的变量,函数内外会始终保持一致,可以对照下面两个列子再加深下印象:
global:
实例代码如下:
<?php function myfunction() { global $bar; unset($bar); } $bar = "someting"; myfunction(); echo $bar; ?>
输出:someting
实例代码如下:
$GLOBALS[]:
<?php function foo() { unset($GLOBALS['bar']); } $bar = "something"; foo(); echo $bar; ?>
输出:空
当按照上面的思路理解后,碰到下面的情况是不是又有些晕呢?
实例代码如下:
<?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>
输出将是 "3".在函数中申明了全局变量 $a 和 $b,任何变量的所有引用变量都会指向到全局变量.
怎么不是2呢,在函数外部不是不影响吗,请注意$b在函数中并没有通过引用修改,而是修改的$b指向物理内存的值,因此外部输入为3

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

AI Hentai Generator
Generate AI Hentai for free.

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



Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:
![Internal error: Unable to create temporary directory [Resolved]](https://img.php.cn/upload/article/000/000/164/168171504798267.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

PHP function introduction—strpos(): Check whether a variable is a string In PHP, is_string() is a very useful function, which is used to check whether a variable is a string. When we need to determine whether a variable is a string, the is_string() function can help us achieve this goal easily. Below we will learn about how to use the is_string() function and provide some related code examples. The syntax of the is_string() function is very simple. it only needs to

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint
