The difference between global variables and $globals[in PHP _php tutorial

Source: Internet
Author: User
PHP's global variables are a little bit different from the C language. In the C language, global variables are active in functions unless they are shrouded by local variables. This may cause some problems, and some people may inadvertently change a global variable. In PHP, global variables must be globally declared as global when applied in a function.

An example of comparison
Example one:

The code is as follows Copy Code
$var 1 = 1;
function Test () {
unset ($GLOBALS [' var1 ']);
}
Test ();
echo $var 1;
?>


Because $var1 was removed, nothing was printed.
Example two:

The code is as follows Copy Code
$var 1 = 1;
function Test () {
Global $var 1;
Unset ($var 1);
}
Test ();
echo $var 1;
?>


Accidentally printed 1. Proof that the deletion is only an alias reference, and that its value is not changed by any of its own.

Second, explain
The global $var is actually the & $GLOBALS [' var '], calling an alias of an external variable.
The $var1 and $globals[' var1 ' in the code above refer to the same variable, not two different variables.
PHP's global variables are a little bit different from the C language. In the C language, global variables are active in functions unless they are shrouded by local variables. This may cause some problems, and some people may inadvertently change a global variable. In PHP, global variables must be globally declared as global when applied in a function.
PHP's global variable is defined as a global variable, but the global variable is not applied to the entire Web site, but is applied to the current page, including all files of include or require.

Iii. Conclusion
1. $GLOBALS [' var '] is the external global variable itself
2.global $var is a reference or pointer to an external $var with the same name.

http://www.bkjia.com/PHPjc/632199.html www.bkjia.com true http://www.bkjia.com/PHPjc/632199.html techarticle PHP's global variables are a little bit different from the C language. In the C language, global variables are active in functions unless they are shrouded by local variables. This may cause some problems, some people may not ...

  • Related Article

    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.