w

http://php.net/manual/zh/language.variables.scope.php

http://php.net/manual/en/language.variables.scope.php

0-

<?php
$a = 1;
$b = 2;
var_dump($GLOBALS);
array(8) {
["GLOBALS"]=>
*RECURSION*
["_POST"]=>
array(0) {
}
["_GET"]=>
array(0) {
}
["_COOKIE"]=>
array(0) {
}
["_FILES"]=>
array(0) {
}
["_SERVER"]=>
array(34) {
["HTTP_HOST"]=>
string(9) "localhost"
["HTTP_CONNECTION"]=>
string(10) "keep-alive"
["HTTP_CACHE_CONTROL"]=>
string(9) "max-age=0"
["HTTP_UPGRADE_INSECURE_REQUESTS"]=>
string(1) "1"
["HTTP_USER_AGENT"]=>
string(114) "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36"
["HTTP_ACCEPT"]=>
string(74) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
["HTTP_ACCEPT_ENCODING"]=>
string(23) "gzip, deflate, sdch, br"
["HTTP_ACCEPT_LANGUAGE"]=>
string(44) "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,ja;q=0.2"
["PATH"]=>
string(252) "C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\Anaconda2;C:\Program Files\Anaconda2\Scripts;C:\Program Files\Anaconda2\Library\bin;D:\phpStudy\php53;"
["SystemRoot"]=>
string(10) "C:\windows"
["COMSPEC"]=>
string(27) "C:\windows\system32\cmd.exe"
["PATHEXT"]=>
string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"
["WINDIR"]=>
string(10) "C:\windows"
["SERVER_SIGNATURE"]=>
string(0) ""
["SERVER_SOFTWARE"]=>
string(47) "Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/5.3.29"
["SERVER_NAME"]=>
string(9) "localhost"
["SERVER_ADDR"]=>
string(3) "::1"
["SERVER_PORT"]=>
string(2) "80"
["REMOTE_ADDR"]=>
string(3) "::1"
["DOCUMENT_ROOT"]=>
string(15) "D:/phpStudy/WWW"
["REQUEST_SCHEME"]=>
string(4) "http"
["CONTEXT_PREFIX"]=>
string(0) ""
["CONTEXT_DOCUMENT_ROOT"]=>
string(15) "D:/phpStudy/WWW"
["SERVER_ADMIN"]=>
string(18) "admin@phpStudy.net"
["SCRIPT_FILENAME"]=>
string(33) "D:/phpStudy/WWW/new/php/class.php"
["REMOTE_PORT"]=>
string(5) "65446"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["REQUEST_METHOD"]=>
string(3) "GET"
["QUERY_STRING"]=>
string(0) ""
["REQUEST_URI"]=>
string(18) "/new/php/class.php"
["SCRIPT_NAME"]=>
string(18) "/new/php/class.php"
["PHP_SELF"]=>
string(18) "/new/php/class.php"
["REQUEST_TIME"]=>
int(1491535570)
}
["a"]=>
int(1)
["b"]=>
int(2)
}

$GLOBALS 是一个关联数组,每一个变量为一个元素,键名对应变量名,值对应变量的内容。$GLOBALS 之所以在全局范围内存在,是因为 $GLOBALS 是一个超全局变量

The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.

1-

<?php
$a = 1;
$b = 2; function Sum()
{
global $a, $b; $b = $a + $b;
} Sum();
echo $b;

3

以上脚本的输出将是“3”。在函数中声明了全局变量 $a 和 $b 之后,对任一变量的所有引用都会指向其全局版本。对于一个函数能够声明的全局变量的最大个数,PHP 没有限制。

The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function.

<?php
$a = 1; /* global scope */ function Test()
{
echo $a; /* reference to local scope variable */
} Test();

Notice: Undefined variable: a

这个脚本不会有任何输出,因为 echo 语句引用了一个局部版本的变量 $a,而且在这个范围内,它并没有被赋值。你可能注意到 PHP 的全局变量和 C 语言有一点点不同,在 C 语言中,全局变量在函数中自动生效,除非被局部变量覆盖。这可能引起一些问题,有些人可能不小心就改变了一个全局变量。PHP 中全局变量在函数中使用时必须声明为 global。

This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function.

超全局变量

access variables from the global scope 在全局范围内访问变量的2种方法的更多相关文章

  1. 全局流水ID号生成的几种方法

    这个问题源自于,我想找一个分布式下的ID生成器.  这个最简单的方案是,数据库自增ID.为啥不用咧?有这么几点原因,一是,会依赖于数据库的具体实现,比如,mysql有自增,oracle没有,得用序列, ...

  2. python之GIL官方文档 global interpreter lock 全局解释器锁

    0.目录 2. 术语 global interpreter lock 全局解释器锁3. C-API 还有更多没有仔细看4. 定期切换线程5. wiki.python6. python.doc FAQ ...

  3. 前端测试框架Jest系列教程 -- Global Functions(全局函数)

    写在前面: Jest中定义了很多全局性的Function供我们使用,我们不必再去引用别的包来去实现类似的功能,下面将列举Jest中实现的全局函数. Jest Global Functions afte ...

  4. 【摄像头】Global Shutter(全局快门)与Rolling Shutter(卷帘快门)的区别与比较

    由于红外补光灯的爆闪,所以一般DMS会用global shutter的sensor,而不是rolling shutter的. 参考 1. Global Shutter(全局快门)与Rolling Sh ...

  5. python27期day10:函数的动态参数、函数的注释、函数的名称空间、函数的嵌套、global(修改全局的)、nonlocal(修改局部的)、函数名的第一类对象及使用、作业题。

    1.动态参数的作用: 能够接收不固定长度参数 位置参数过多时可以使用动态参数 * args是程序员之间约定俗称(可以更换但是不建议更换) * args获取的是一个元组 ** kwargs获取的是一个字 ...

  6. php部分(查看文件、建立站点、语法变量、变量的几个方法、“全局局部变量的调用”、static、函数参数的作用域);

    浏览器查看php文件: 建立站点,浏览php文件: php的语法 <?php echo "Hello World!"; ?> 注释语法: <?php // 这是 ...

  7. [ python ] 全局和局部作用域变量的引用

    全局与局部变量的引用 (a)locals(b)globals 这里还需要在补充2个关键字一起比较学习,关键字:(c)nonlocal(d)global locals 和 globals locals: ...

  8. YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法

    上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...

  9. 让程序只运行一个实例(Delphi篇)(三种方法,其中使用全局原子的方法比较有意思)

    Windows 下一个典型的特征就是多任务,我们可以同时打开多个窗口进行操作,也可以同时运行程序的多个实例,比如可以打开许多个资源管理器进行文件的移动复制操作.但有时出于某种考虑(比如安全性),我们要 ...

随机推荐

  1. CENTOS 下安装APK反编译工具 APKTOOL

    转于:http://www.qiansw.com/centos-apk-apktool.html 我使用的是CentOS6.4 64位的系统.首先需要下载两个包.这里下载:https://code.g ...

  2. vivado笔记

    Vivado主界面 Vivado套件,相当于把ISE.ISim.XPS.PlanAhead.ChipScope和iMPACT等多个独立的套件集合在一个Vivado设计环境中,在这个集合的设计流程下,不 ...

  3. Windows7光盘制作: 向脱机映像添加补丁

    所谓脱机(offline)映像就是WIM文件,install.wim就是脱机映像,有的install.wim里面有几个子映像,有的里面就只有一个.install.wim中有多个映像的光盘叫做多合一光盘 ...

  4. OpenERP report doesn't work

    1. When you have used OpenOffice edited  one of reports,it has stored the report's banary data is da ...

  5. IOS证书之Certificates,Devices, Identifiers & Profiles

    做IOS开发的,在需要发布应用的时候,会接触到iOS Dev Center里面的证书制作,按照网上的资料操作,我们可以很容易的制作证书并且完成真机调试或者是产品发布,但是对于Certificates. ...

  6. hdu6078 Wavel Sequence dp+二维树状数组

    //#pragma comment(linker, "/STACK:102400000,102400000") /** 题目:hdu6078 Wavel Sequence 链接:h ...

  7. 同学帮帮移动 H5 弹出层类组件:txbb-pop

    Txbb.Pop 同学帮帮弹出层类组件,简洁.无依赖,使用 CSS3 实现动画效果. 为什么要再造一遍轮子 弹出层是常见的业务场景,而且弹出层的业务场景很简单,没必要使用大而全的库,并且,我们经常会有 ...

  8. Linux利器:WinSCP,Putty,pscp和psftp

    # Summary PuTTY (Telnet 和 SSH 客户端) PSCP (SCP 客户端, 命令行下通过 SSH 拷贝文件,类似于 Unix/Linux 下的 scp 命令) PSFTP (S ...

  9. 动态添加js的方法

    var Skip={};//获取XMLHttpRequest对象(提供客户端同http服务器通讯的协议)Skip.getXmlHttpRequest=function (){ if ( window. ...

  10. Hadoop分布式文件系统--HDFS结构分析

    转自:http://blog.csdn.net/androidlushangderen/article/details/47377543 HDFS系列:http://blog.csdn.net/And ...