PHP $GLOBALS Hyper-Global variable Analysis _php tutorial

Source: Internet
Author: User
Tags smarty template
PHP has a super global variable $globals that is not used by many people. Reasonable use of this variable can make the work more efficient, this article mainly analyzes the use of this super-global variable and the difference between $globals and global.

$GLOBALS definition: References all the variables available in the global scope (a globally combined array containing all the variables). The name of the variable is the key to the array, and unlike all other hyper-global variables, $GLOBALS is always available anywhere in the PHP code, and you can tell by printing the result of the $globals variable.

In the PHP life cycle, the so-called global variables that are defined outside the function body are not directly available inside the function. If you want to access externally defined global variables within the function body, you can use the global declaration or directly using $globals to access them, for example:

 
  '; global $var 1;echo $var 1, '
'; echo $GLOBALS [' var2 '];}

The result will be printed as:

Taobao
Www.phpernote.com
www.google.cn

Here are the main explanations for the difference between global and $globals:

$GLOBALS [' var '] is the external global variable itself, and the global $var is an external $var reference or pointer, which means that global has a function that produces an alias variable that points to the external variable of the function, rather than the actual external variable of the function, and $globals[] It is true that the external variables are called, and the functions are always consistent inside and outside the function. Here's an example of how this is done:

$var 1=1; $var 2=2;function Test () {$GLOBALS [' var2 ']=& $GLOBALS [' var1 '];}  Test (); Echo $var 2;

Print result is 1

$var 1=1; $var 2=2;function Test () {global $var 1, $var 2; $var 2=& $var 1;}  Test (); Echo $var 2;

The print result is 2. Why would I print a result of 2? This is because $VAR1 's reference points to the $VAR2 's reference address. Causes the value of the substance to not change. Let's look at one more example.

$var 1=1;function Test () {unset ($GLOBALS [' var1 ']);}  Test (); Echo $var 1;

Because $var1 was removed, nothing was printed.

$var 1=1;function Test () {Global $var 1;unset ($var 1);}  Test (); Echo $var 1;

The print result is 1. To prove that the deletion is only an alias | reference, the value of itself has not been changed. This means that global $var is actually $var=& $GLOBALS [' var ']. Call an alias of an external variable.

Articles you may be interested in

    • Summary of System variables commonly used in thinkphp templates
    • Use PHP functions in Smarty Templates and how to use multiple functions for a variable in a smarty template
    • PHP commonly used to determine the function of variables
    • Differences in PHP after adding the static keyword to variables and functions
    • PHP uses Curl Functions to crawl Web pages and download files in multiple threads
    • PHP built-in variable directory_separator parsing
    • PHP output Yesterday, today, tomorrow is the day of the week method
    • Summary of system constants in the thinkphp Action controller

http://www.bkjia.com/PHPjc/764106.html www.bkjia.com true http://www.bkjia.com/PHPjc/764106.html techarticle PHP has a super global variable $globals that is not used by many people. Reasonable use of this variable can make the work more efficient, this article mainly analyzes the use of this super-global variable ...

  • 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.