[Basic PHP knowledge] $ GLOBALS [''] and global difference _ PHP Tutorial

Source: Internet
Author: User
[Basic PHP knowledge] $ GLOBALS [] is different from global. In php program development, many developers did not notice the difference between $ GLOBALS [] and global. In fact, the difference between the two writing methods is quite different, not just literal, next I will understand the differences between $ GLOBALS [] and global in php program development. In fact, the differences between the two writing methods are quite different, not just literal differences, let me take a look at their specific differences.

Differences

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

2. global $ var is an external $ var reference or pointer with the same name (it can be understood as an external $ var replacement ).

For example:

Copy to ClipboardReference: [www.bkjia.com] $ Var1 = "test1 ";
$ Var2 = "test2 ";
Function test (){
$ GLOBALS ['var2'] = & $ GLOBALS ['var1'];
}
Test ();
Echo $ var2; // output test1
?>

The output result of the above code is test1.

Copy to ClipboardReference: [www.bkjia.com] $ Var1 = "test1 ";
$ Var2 = "test2 ";
Function test (){
Global $ var1, $ var2;
$ Var2 = & $ var1;
}
Test ();
Echo $ var2; // output test2
?>

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

Why does test2 output? In fact, the reference of $ var1 points to the reference address of $ var2 (in general, $ var1 in the test function is a substitute ). The actual value is not changed.

Let's look at an example.

Copy to ClipboardReference: [www.bkjia.com] $ Var1 = "test1 ";
Function test (){
Unset ($ GLOBALS ['var1']);
}
Test ();
Echo $ var1; // no output
?>

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

Copy to ClipboardReference: [www.bkjia.com] $ Var1 = "test1 ";
Function test (){
Global $ var1;
Unset ($ var1 );
}
Test ();
Echo $ var1; // output test1
?>

Test1 was unexpectedly output this time. It indicates that only the alias or reference (substitute) is deleted, and the value of the variable itself is not changed.

Do you understand?

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

...

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.