Home  >  Article  >  Backend Development  >  Detailed explanation of examples of global and $GLOBALS[] in php

Detailed explanation of examples of global and $GLOBALS[] in php

零下一度
零下一度Original
2017-06-29 13:59:181223browse

PHP’s global variables are a little different from C language. In C language, global variables automatically take effect in functions unless overridden by local variables

This may cause some problems, and some people may be careless. Change a global variable. Global variables in PHP must be declared global when used in a function (note that the Global keyword is only useful when defined in a function).
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; //If $a is not defined as a global variable, the function body cannot access $a outside the function body, but you can define a same name $a, so When this variable is a local variable, it is equivalent to a local variable in C language and can only be used inside the function body.

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; //错误, 
}


//Look at the following example again

function f() 
{ 
global $a; 
$a=123; 
} 
f();


echo $a; //Correct, you can use


2: Global problem analysis:
question: I have defined some variables ($a) in config.inc.php, and include ("config.inc.php") outside the function in other files. These variables $a need to be used inside the function. If not If declared, 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:
Example Use $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's content. $GLOBALS exists in the global scope 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, in a file, as long as you declare it as global $db, then below 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"; 
?>


Copy code
The execution result is:
0
5
Why is this happening? Shouldn't it be two 5s? How come there are 1 0 and 1 5? ?
Well, we keep the above questions and analyze the principles of $GLOBALS and global in depth!
We all know that variables are actually the "codenames" of the corresponding physical memory in the code. Assume that the three variables we declared above are allocated The memory is represented as follows:
Quoting the explanation of $GLOBALS in the PHP manual:
Global variable: $GLOBALS
Note: $GLOBALS is applicable in PHP 3.0.0 and later versions.
An array composed 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; ?>

执行结果为: 

为什么会输出1呢?不是已经把$a给unset了吗?unset失灵了?php的bug? 
都不是,其实unset起作用了,是把test函数中的$a给unset掉了,可以在函数后面加入 
print $a; 
复制代码 
来测试!也就是说global产生了test函数外部$a的别名变量“$a”,为了和外面的$a区别,我把它成为--test->$a,那么例子1也这么命名的话,可得出下面的图: 
而test_globals执行过以后,看变量的变化: 
此时,看图,就能理解为什么例子1执行完以后,$var2是0,而$var3是5了! 
所以我们得出一个结论,在函数中global和$GLOBALS[]的区别在于: 
global在函数产生一个指向函数外部变量的别名变量,而不是真正的函数外部变量,一但改变了别名变量的指向地址,就会发生一些意料不到情况(为什么会打印结果为2呢?其实就是因为$var1的引用指向了$var2的引用地址。导致实质的值没有改变。这时候只是指向$var1的指针指向了$var2的指针,只是指针指向变了一下,但是实质上根本就没有改变$var2的值,因此$var2的值仍旧不会变化),例如例子1. 
$GLOBALS[]确确实实调用是外部的变量,函数内外会始终保持一致! 
注:(接着回到上面的例子1,看test_global中的这一代码“$var2 =& $var1;”,上面是一个引用赋值运算,也就是$var2将指向var1所指向的物理内存地址,所以例子1执行过test_global函数以后,变量的变化只在函数的局部产生效应,在函数外部$var2的指向物理内存地址并没有变化,还是它自己.(重点) 
接着回到上面的例子1,看test_global中的这一代码“$var2 =& $var1;”,上面是一个引用赋值运算,也就是$var2将指向var1所指向的物理内存地址,所以例子1执行过test_global函数以后,变量的变化由下图可以看出) 

The above is the detailed content of Detailed explanation of examples of 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