Home  >  Article  >  Backend Development  >  What is the difference between global and $GLOBALS[] in php?

What is the difference between global and $GLOBALS[] in php?

伊谢尔伦
伊谢尔伦Original
2017-06-21 17:34:032400browse

1: The function of Global is to define global variables, but this global variable does not apply to the entire website, but to the current page, including all files in include or require.

<?php 
$a=123; 
function aa() 
{ 
Global $a; //如果不把$a定义为global变量,函数体内是不能访问函数体外部的$a的,但是可以定义一个相同的名字$a,此时这个变量是局部变量,等同于C语言的局部变量,只能在函数体内部使用。 
echo $a; 
} 
aa(); 
?>

Summary: Global variables defined in the function body can be used outside the function body, but global variables defined outside the function body cannot be used inside the function body.

$global $a; 
$a=123; 
function f() 
{ 
echo $a; //错误, 
} 
//再看看下面一例 
function f() 
{ 
global $a; 
$a=123; 
} 
f(); 
echo $a; //正确,可以使用

2: Global problem analysis:
question: I defined some variables ($a) in config.inc.php, and included ("config.inc.php") outside the function in other files. These variables need to be used inside the function. $a, if there is no declaration, echo $a will not print anything. Therefore, global $a is declared, but there are many functions and many variables. You can't declare it like this repeatedly, right? If there is any good solution, please give me some advice.
answer1: First define the constants in config.inc.php: define(constant name, constant value)
Then require 'config.inc.php' in other places where it needs to be used,
Then you can This constant is used directly in this file.
answer2: I also have a way, which is to define an array, such as $x[a], $x, so that you only need to declare global $x.
answer3: I tried your method, but it didn’t work.
answer4: Change your php.ini file.
3. Some examples of Global and $GLOBALS arrays
Example: using global

<?php 
$w3sky = 1; 
$w3sky2 = 2; 
function Sum() 
{ 
global $w3sky, $w3sky2;$w3sky2 = $w3sky + $w3sky2; 
}Sum(); 
echo $w3sky2; 
?>

The output of the above script will be "3". Global variables $w3sky and $w3sky2 are declared in the function, and all reference variables of any variable will point to the global variables. PHP has no limit on the maximum number of global variables that a function can declare.
The second way to access variables in the global scope is to use a special PHP custom $GLOBALS array. The previous example can be written as:
The example uses $GLOBALS instead of global

<?PHP 
$w3sky = 1; 
$w3sky2 = 2;function Sum() 
{ 
$GLOBALS[&#39;w3sky&#39;] = $GLOBALS[&#39;w3sky&#39;] + $GLOBALS[&#39;w3sky2&#39;]; 
}Sum(); 
echo $w3sky2; 
?>

In the $GLOBALS array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the variable Content. The reason $GLOBALS exists in the global scope is because $GLOBALS is a superglobal variable. The following example shows the use of super global variables:
Example demonstrates examples of super global variables and scope

<?PHP 
function test_global() 
{ 
// 大多数的
预定义变量
并不 "super",它们需要用 &#39;global&#39; 关键字来使它们在函数的本地区域中有效。 
global $HTTP_POST_VARS;echo $HTTP_POST_VARS[&#39;name&#39;];// Superglobals 在任何范围内都有效,它们并不需要 &#39;global&#39; 声明。Superglobals 是在 PHP 4.1.0 引入的。 
echo $_POST[&#39;name&#39;]; 
} 
?>

global That is to say, as long as you declare it as global $db in a file, then under the declaration You can then reference this $db.
4. I originally thought that global and $GLOBALS were the same except for the different writing methods. However, in practical applications, I found that the difference between the two is still very big!
Look at the following example first:

<?php 
// 例子1 
function test_global() { 
global $var1, $var2; 
$var2 =& $var1; 
} 
function test_globals() { 
$GLOBALS[&#39;var3&#39;] =& $GLOBALS[&#39;var1&#39;]; 
} 
$var1 = 5; 
$var2 = $var3 = 0; 
test_global(); 
print $var2 ."\n"; 
test_globals(); 
print $var3 ."\n"; 
?>

The execution result is:

0 
5

How could this happen? Shouldn’t it be two 5s? How could there be 1 0 and 1 5?
Well, we keep the above Question, in-depth analysis of the principles of $GLOBALS and global!
We all know that variables are actually the "codenames" of the corresponding physical memory in the code. Assume that the memory allocated by the three variables we declared above is represented by the following figure:
Quote php manualExplanation of $GLOBALS:
Global variable: $GLOBALS
Note: $GLOBALS is applicable in PHP 3.0.0 and later versions.
An array consisting of all defined global variables. The variable name is the index into the array.
This is a "superglobal", or can be described as an automatic global variable.
That is to say, $var1 and $GLOBALS['var1'] in the above code refer to the same variable, not 2 different variables!
Let’s analyze what global has done?
We all know that the variables generated by functions in php are private variables of the function, so the variables generated by the global keyword will definitely not escape this rule. Why do you say this? Look at the following code:

<?php 
// 例子2 
function test() { 
global $a; 
unset($a); 
} 
$a = 1; 
test(); 
print $a; 
?>

The execution result is: 1
Why is 1 output? Hasn't $a been unset? Unset failed? PHP bug?
No, in fact unset works. It unsets $a in the test function. You can add
print $a;
Copy the code to test! In other words, global generates the alias variable "$a" of $a outside the test function, and after test_globals is executed, look at the changes in the variable.
At this point you can understand why after example 1 is executed, $var2 is 0 and $var3 is 5!
So we come to a conclusion. The difference between global and $GLOBALS[] in a function is:
global generates an alias variable in the function that points to the external variable of the function, instead of the real external variable of the function. Once it changes Without the pointing address of the alias variable, some unexpected situations will occur (why is the printed result 2? In fact, it is because the reference of $var1 points to the reference address of $var2. As a result, the actual value has not changed. At this time, it just points to The pointer of $var1 points to the pointer of $var2, but the pointer pointer changes slightly, but the value of $var2 does not actually change at all, so the value of $var2 still does not change), for example, Example 1.
$GLOBALS [] It is true that the call is an external variable, and the function will always be consistent inside and outside!

The above is the detailed content of What is the difference between global and $GLOBALS[] in php?. For more information, please follow other related articles on the PHP Chinese website!

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