search
Homephp教程php手册PHP Global变量定义一些用法详解

PHP Global变量定义一些用法详解

Jun 13, 2016 am 10:04 AM
globalphpeffectvariabledefinitionrightyesusageofparseDetailed explanation

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


 代码如下 复制代码

$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 文件

 代码如下 复制代码

function Test_Global()

    Test(); 

include 'B.php';   //将include 从局部Test_Global函数中移出
$a = 0 ;
Test_Global();
echo $a;
?>
//B.php 文件
function Test()
{
    global $a;
    $a =1;
}
?>

2.优秀的访问器

 代码如下 复制代码

//A.php 文件
include 'B.php';
$a =0;
Set_Global($a);
echo $a;
?>
//B.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'],调用外部变量的一个别名而已!

Statement
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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools