The difference between hyper-global variables $globals and global in PHP

Source: Internet
Author: User
Tags array exit execution functions include variables variable access

This article is about the difference between hyper-global variables $globals and Global in PHP.

One, super global variable $globals

There are many PHP hyper-global variables, such as the following are super global variables (superglobal):

$GLOBALS, $_server,$_get,$_post,$_files,$_cookie,$_session,$_request,$_env.

Official Description:

$GLOBALS-References all variables available in the global scope.

A global combined array that contains all the variables. The name of the variable is the key of the array.

The global variable that appears, can be obtained by $globals this array.

In the PHP lifecycle, the so-called global variables defined outside the function body are not directly available within the function.

$foo = "Example content";
Test ();
function test () {
    $foo = "local variable";
    Echo ' $foo in the current scope: '. $foo. <br> ";
    Echo ' $foo in global scope: '. $GLOBALS ["foo"]. " <br> ";
}

As in the example above, to access the external $foo must use the $GLOBALS array. It is also true for external global variables that come in through the include file.

Global also has this function in PHP, and it differs from $globals in that:

Global creates an alias variable in the function that points to the external variable of the function, rather than the actual function external variable.

$GLOBALS [] The actual call is an external variable, and the function is always consistent inside and outside.

For a member variable in a class, a function in a class must be accessed in a $this-> way, not in a $globals way:

Global is defined globally, but this global variable is not applied to the entire Web site, but is applied to the current page, including all files with include or require.

Second, the example explanation

function T1 () {
    global $var 1, $var 2;
    $var 2=& $var 1;
}
Function T2 () {
    $GLOBALS [' Var3 ']=& $GLOBALS [' var1 '];
}
$var 1=5;
$var 2= $var 3=0;
T1 ();
Print $var 2. " \ n ";
T2 ();
Print $var 3. " \ n ";

The results of the execution are:

0
5

Why not 2 5 but a 0 and a 5? Then revise the example:

function T1 () {
    global $var 1;
    $var 1=2;
    Unset ($var 1);
}
Function T2 () {
    $GLOBALS [' var1 ']=3;
    unset ($GLOBALS [' var1 ']);
}
$var 1=1;
T1 ();
Print $var 1. " \ n ";
T2 ();
Print $var 1. " \ n ";

The result of the execution is only one 2;

1, $GLOBALS is an array of all defined global variables automatically formed. The variable name is the index of the array. That is, $globals[' var1 '] is the same variable as the variable $var1 outside the function, so after the $globals[' var1 ' is deleted, the variable no longer exists and all cannot be exported.

Note: $GLOBALS is an automatic global variable. This means that it works in all scripts. You do not need to use the global $GLOBALS in a function or method to access it.

2, "Global $var 1;" is the alias variable "$var 1" that produces the external $var1 of the function, it is not a real function external variable, he only exists inside the function, so even if you delete the alias variable within the function, it will not affect the outside variables, but you can modify the value of the function external variable.

Perhaps some people always want to know the difference between this or that:

In the PHP program, including other programs learning, hands-on experiments, based on the results added to the thinking, sometimes more than online search may come faster, more accurate. Now let's talk about what to do with PHP accessing variables globally.

Example one: global defines globally variable.

function Test_global () {
    global $var 1;
    $var 1= ' OK ';
    Unset ($var 1);
}
Test_global ();
$var 2=& $var 1;
Unset ($var 1);
echo $var 2;

First do not give the result, run the program yourself. The variables inside the function can be accessed. As can be seen from the results, unset just disconnected the variable name and variable value, and did not immediately destroy the value of the variable, but also defined within the function of the global variable, the actual outside only use the alias inside the function, so we can still access $var1 outside.

Example two: $GLOBALS the variables defined outside the function inside the function.

$waibu = ' out ';
function ff () {
    echo $GLOBALS [' Waibu '];
}
FF ();

Using $waibu directly inside a function can be an error.

Articles that you may be interested in

    • The difference between a variable and a function in PHP after adding the static keyword
    • Analysis of super global variables for PHP $GLOBALS
    • Use and difference in PHP to jump out of multiple loops using Break,continue,goto,return,exit
    • The difference and usage of return and exit, break and Contiue in PHP
    • Differences between synchronous and asynchronous requests in HTTP requests
    • Usage and difference of echo,print,print_r,var_export,var_dump in PHP
    • Use PHP functions in Smarty Templates and how to use multiple functions for a variable in smarty templates
    • How to block event bubbling in jquery and its difference


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.