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:windowssystem32;C:windows;C:windowsSystem32Wbem;C:windowsSystem32WindowsPowerShellv1.0;C:Program FilesTortoiseSVNin;C:Program FilesAnaconda2;C:Program FilesAnaconda2Scripts;C:Program FilesAnaconda2Libraryin;D:phpStudyphp53;"
    ["SystemRoot"]=>
    string(10) "C:windows"
    ["COMSPEC"]=>
    string(27) "C:windowssystem32cmd.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.

超全局变量

原文地址:https://www.cnblogs.com/rsapaper/p/6677290.html