The difference between $globals and global in PHP Super global variables

Source: Internet
Author: User

The difference between $globals and global in PHP Super global variables

First, super global variable $globals

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

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

Official Note:

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

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

That is, the global variables that have occurred can be obtained by $globals this array.

In the PHP life cycle, the so-called global variables that are defined outside the function body cannot be obtained directly inside the function.

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

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

In PHP, global also has this function, which differs from $GLOBALS in that:

Global in the function produces an alias variable that points to the external variable of the function, not the actual external variable of the function.

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

For a member variable in a class, the function in the class must be accessed using $this->, and cannot be used in $globals mode:

The role of global is to define globals, but this global variable is not applied to the entire site, but to the current page, including all files of 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 ();p rint $var 2. " \ n "; T2 ();p rint $var 3." \ n ";

The result of the execution is:

05

Why not 2 5 instead of a 0 and a 5?

Revise the example again:

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

The execution result is entered only one 2;

$GLOBALS is an array that is automatically formed by all defined global variables. 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 when the $globals[' var1 ' is removed, the variable no longer exists and all cannot be output.

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.

"Global $var 1;" is the alias variable "$var 1" that produced the external $var1 of the function

It is not a real function external variable, he only exists inside the function, so even if the alias variable is deleted within the function does not affect the outside variables, but can modify the value of the function external variables.

Perhaps some people always want to know the difference between this or that: in the PHP program, including other programs in the study, self-experiment, according to the results of thinking, sometimes more than the Internet search may come faster, more accurate. Let's take a look at the global scope of PHP access to variables to do?

Example one: Global defines a 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;

Do not give the results first, run the program yourself. Variables inside the function can be accessed.

As can be seen from the results, unset just disconnects the variable name from the value of the variable, does not immediately destroy the value of the variable, and the global variable defined inside the function, the actual external only uses the alias inside the function, so we can still access the $var1 outside.

Example two: $GLOBALS a variable defined outside the function to access the function.

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

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

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