Home > Backend Development > PHP Tutorial > [Basic knowledge of PHP] The difference between $GLOBALS[''] and global_PHP tutorial

[Basic knowledge of PHP] The difference between $GLOBALS[''] and global_PHP tutorial

WBOY
Release: 2016-07-21 14:58:10
Original
988 people have browsed it

In PHP program development, many developers do not notice the difference between $GLOBALS[] and global. The two ways of writing are actually very different, not just literal differences. Let me take a look. Their specific differences.

Specific differences

1.$GLOBALS['var'] is the external global variable itself (the actual external $var itself).

2.global $var is a reference or pointer of the same name as an external $var (can be understood as a substitute for an external $var).

Let’s give an example:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $var1 = "test1";
$var2 = "test2";
function test(){
$GLOBALS['var2'] = &$GLOBALS['var1' ];
}
test();
echo $var2; // Will output test1
?>

The output result of the above code is test1

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $var1 = "test1";
$var2 = "test2";
function test(){
global $var1,$var2;
$var2 = &$ var1;
}
test();
echo $var2; // Will output test2
?>

The output of the above code is a bit unexpected, the result is test2

Why is test2 output? In fact, it is because the reference of $var1 points to the reference address of $var2 (in layman's terms, $var1 in the test function is a substitute). The resulting value of the substance has not changed.

Let’s look at another example.

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $var1 = "test1";
function test(){
unset($GLOBALS['var1']);
}
test();
echo $var1; // Nothing is output
?>

Because $var1 has been truly deleted, nothing can be output.

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $var1 = "test1";
function test(){
global $var1;
unset($var1);
}
test();
echo $var1; // Output test1
?>

This time test1 was unexpectedly output. It proves that only the alias or reference (substitute) is deleted, and the value of the variable itself is not changed in any way.

Understand?

In other words, global $var is actually an alias for $var = &$GLOBALS['var'] to call external variables.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363889.htmlTechArticleIn PHP program development, many developers do not notice the difference between $GLOBALS[] and global. There is actually a big difference between the two ways of writing, and it is not just a literal difference. Let me understand below...
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