"PHP Basics" $GLOBALS ["] and global differences _php tutorial

Source: Internet
Author: User
Tags php basics
In the development of PHP program, many developers did not notice the difference between $globals[] and global, these two kinds of writing is actually very different, not just the literal above differences, let me look at their specific differences.

Specific differences

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

2.global $var is an external $var reference or pointer (which can be understood as an external $var alias).

To illustrate:

Copy to ClipboardWhat to refer to: [www.bkjia.com] $var 1 = "test1";
$var 2 = "test2";
function Test () {
$GLOBALS [' var2 '] = & $GLOBALS [' var1 '];
}
Test ();
echo $var 2; The output test1
?>

The output of the above code is TEST1

Copy to ClipboardWhat to refer to: [www.bkjia.com] $var 1 = "test1";
$var 2 = "test2";
function Test () {
Global $var 1, $var 2;
$var 2 = & $var 1;
}
Test ();
echo $var 2; The output test2
?>

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

Why does it output test2? In fact, it is because $var1 refers to the $var2 of the reference address (popular in the test function of the $VAR1 is an alias). Causes the value of the substance to not change.

Let's look at one more example.

Copy to ClipboardWhat to refer to: [www.bkjia.com] $var 1 = "test1";
function Test () {
unset ($GLOBALS [' var1 ']);
}
Test ();
echo $var 1; I can't output anything.
?>

Because the $var1 has been really deleted, so what things can not output.

Copy to ClipboardWhat to refer to: [www.bkjia.com] $var 1 = "test1";
function Test () {
Global $var 1;
Unset ($var 1);
}
Test ();
echo $var 1; Output test1
?>

This time again unexpectedly output the test1. It proves that the deletion is only an alias or reference (a surrogate), and that the value of the variable itself is not changed.

Do you understand?

That is, the global $var is actually $var = & $GLOBALS [' var '] calls an alias of an external variable.

http://www.bkjia.com/PHPjc/363889.html www.bkjia.com true http://www.bkjia.com/PHPjc/363889.html techarticle in the development of PHP program, many developers did not notice the difference between $globals[] and global, these two kinds of writing is actually very different, not just the difference between the literal, below I come to understand ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.