Home > Backend Development > PHP Tutorial > One analysis of global and $GLOBALS[] in php_PHP tutorial

One analysis of global and $GLOBALS[] in php_PHP tutorial

WBOY
Release: 2016-07-21 15:21:34
Original
777 people have browsed it

This can cause problems, as someone might carelessly 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 role 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.

Copy code The code is as follows:

$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 it can define a same name $a. At this time, this Variables are local variables, which are equivalent to local variables 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. Global variables defined outside the body cannot be used within the function body.
Copy code The code is as follows:

$global $a;
$a=123;
function f()
{
echo $a; //Error,
}
//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 defined some variables ($a) in config.inc.php, and in other files, the function includes ("config.inc.php") externally, which is required inside the function. Using these variables $a, echo $a will not print anything if it is not declared. 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 and it didn’t work.
answer4: Change your php.ini file.
3. Some examples of Global and $GLOBALS arrays

Example: Use global
Copy code The code is as follows:

$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
Copy the code The code is as follows:

$w3sky = 1;
$w3sky2 = 2; function Sum()
{
$GLOBALS['w3sky'] = $GLOBALS['w3sky'] + $GLOBALS['w3sky2' ];
}Sum();
echo $w3sky2;
?>

In the $GLOBALS array, each variable is an element, and the key name corresponds to the variable name , the value corresponds to the content of the variable. $GLOBALS exists in the global scope because $GLOBALS is a superglobal variable. The following example shows the use of superglobal variables:
Example demonstrating superglobal variables and scope examples
Copy code The code is as follows:

function test_global()
{
// Most predefined variables are not "super", they need to use the 'global' keyword to make them in the function Valid in the local zone.
global $HTTP_POST_VARS;echo $HTTP_POST_VARS['name'];// Superglobals are valid in any scope, they do not require a 'global' declaration. Superglobals were introduced in PHP 4.1.0.
echo $_POST['name'];
}
?>

global That is to say, in a file, as long as you declare it as global $db, then in the declaration Now you can 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:
Copy code The code is as follows:

// Example 1
function test_global() {
global $var1, $var2;
$var2 =& $var1;
}
function test_globals() {
$GLOBALS['var3'] =& $GLOBALS['var1'];
}
$var1 = 5;
$var2 = $var3 = 0;
test_global();
print $var2 ."n";
test_globals();
print $var3 ."n";
?>

Copy the code
The execution result is:
0
5
How can this happen? Shouldn’t it be two 5s? How come there are 1 0 and 1 5?
Hmm , 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 memory allocated by the three variables we declared above is represented by the following figure:
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 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 certainly cannot escape this rule. Why do we say this? Look at the following code:
Copy code The code is as follows:

// Example 2
function test() {
global $a;
unset($a);
}
$a = 1;
test();
print $a;
?>

Copy code
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! That is to say, global generates the alias variable "$a" for $a outside the test function. In order to distinguish it from the $a outside, I call it --test->$a. Then if Example 1 is also named like this, we can get The following picture appears:
After test_globals is executed, look at the changes in variables:
At this time, looking at the picture, you can understand why after example 1 is executed, $var2 is 0, and $var3 is 5!
So we come to the conclusion that 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!
Note: (Go back to example 1 above and look at the code "$var2 =& $var1;" in test_global. The above is a reference assignment operation, that is, $var2 will point to the physical memory pointed to by var1 Address, so after executing the test_global function in Example 1, the change in the variable only has an effect locally in the function. The physical memory address pointed to by $var2 outside the function does not change, and it is still itself. (Emphasis)
Then go back to the above Example 1, look at the code "$var2 =& $var1;" in test_global. The above is a reference assignment operation, that is, $var2 will point to the physical memory address pointed to by var1, so in Example 1, after executing the test_global function , the changes in variables can be seen in the figure below)
The analysis in this article is not thorough enough. If you don’t understand, please read "Analysis of global and $GLOBALS[] Part 2", which is easy to understand by drawing inferences from one example.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324879.htmlTechArticleThis may cause some problems, some people may change a global variable carelessly. Global variables in PHP must be declared global when used in functions (note that the keyword Global is used in functions...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template