access variables from the global scope 在全局范围内访问变量的2种方法
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种方法的更多相关文章
- 全局流水ID号生成的几种方法
这个问题源自于,我想找一个分布式下的ID生成器. 这个最简单的方案是,数据库自增ID.为啥不用咧?有这么几点原因,一是,会依赖于数据库的具体实现,比如,mysql有自增,oracle没有,得用序列, ...
- python之GIL官方文档 global interpreter lock 全局解释器锁
0.目录 2. 术语 global interpreter lock 全局解释器锁3. C-API 还有更多没有仔细看4. 定期切换线程5. wiki.python6. python.doc FAQ ...
- 前端测试框架Jest系列教程 -- Global Functions(全局函数)
写在前面: Jest中定义了很多全局性的Function供我们使用,我们不必再去引用别的包来去实现类似的功能,下面将列举Jest中实现的全局函数. Jest Global Functions afte ...
- 【摄像头】Global Shutter(全局快门)与Rolling Shutter(卷帘快门)的区别与比较
由于红外补光灯的爆闪,所以一般DMS会用global shutter的sensor,而不是rolling shutter的. 参考 1. Global Shutter(全局快门)与Rolling Sh ...
- python27期day10:函数的动态参数、函数的注释、函数的名称空间、函数的嵌套、global(修改全局的)、nonlocal(修改局部的)、函数名的第一类对象及使用、作业题。
1.动态参数的作用: 能够接收不固定长度参数 位置参数过多时可以使用动态参数 * args是程序员之间约定俗称(可以更换但是不建议更换) * args获取的是一个元组 ** kwargs获取的是一个字 ...
- php部分(查看文件、建立站点、语法变量、变量的几个方法、“全局局部变量的调用”、static、函数参数的作用域);
浏览器查看php文件: 建立站点,浏览php文件: php的语法 <?php echo "Hello World!"; ?> 注释语法: <?php // 这是 ...
- [ python ] 全局和局部作用域变量的引用
全局与局部变量的引用 (a)locals(b)globals 这里还需要在补充2个关键字一起比较学习,关键字:(c)nonlocal(d)global locals 和 globals locals: ...
- YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法
上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把 ...
- 让程序只运行一个实例(Delphi篇)(三种方法,其中使用全局原子的方法比较有意思)
Windows 下一个典型的特征就是多任务,我们可以同时打开多个窗口进行操作,也可以同时运行程序的多个实例,比如可以打开许多个资源管理器进行文件的移动复制操作.但有时出于某种考虑(比如安全性),我们要 ...
随机推荐
- php的instanceof和判断闭包Closure
类型运算符 instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例,在此之前用 is_a(),但是后来 is_a() 被废弃 <?php class MyClass ...
- fs 小计
如果是export 就可以放到b-leg上 如果是set就可以对于a-leg
- thinkphp验证码出不来
import("ORG.Util.Image"); //图像操作类库 ob_end_clean(); $type = isset($_GET['type'])?$_GET['typ ...
- ado连接sql server
//ado连接sql server //头文件加上以下这句. #import "C:\Windows\system\msado15.dll" no_namespace rename ...
- sqlite-在数据库中创建默认时间
create table log( content ), logtime TIMESTAMP default (datetime('now', 'localtime')) )
- 个人的Linux实用命令
Linux下的命令有那么多,我不可能也不想去把每一个都记住,列举一些自己工作中很实用的命令,这些命令或许不是很常用,但是有时候却离不了. 1.网络方面 service iptables start/s ...
- [待解决]ColumnPrefixFilter 不能过滤出全部满足条件的,
Scan scan = new Scan(); ColumnPrefixFilter columnPrefixFilter = new hbase(main)::> scan 't4' ROW ...
- iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- MySql—修改权限
MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant sele ...
- SRM 719 Div 1 250 500
250: 题目大意: 在一个N行无限大的网格图里,每经过一个格子都要付出一定的代价.同一行的每个格子代价相同. 给出起点和终点,求从起点到终点的付出的最少代价. 思路: 最优方案肯定是从起点沿竖直方向 ...