PHP Super global variable $globals and global difference _php tutorial

Source: Internet
Author: User
Tags smarty template
This article shares the differences between $globals and global in the hyper-global variables in PHP.

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:

0
5

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;

1, $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.

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

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

In PHP programs, including other program learning, hands-on experiments, based on 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.

Articles you may be interested in

    • Differences in PHP after adding the static keyword to variables and functions
    • PHP $GLOBALS Hyper-Global variable analysis
    • Usage and differences in PHP that jump out of multiple loops using Break,continue,goto,return,exit
    • The difference and usage of return and exit, break and Contiue in PHP
    • The usage and difference of echo,print,print_r,var_export,var_dump in PHP
    • PHP Output Control In-depth understanding of the difference between Ob_flush and flush
    • PHP empty,is_null,isset The difference between the detailed
    • Use PHP functions in Smarty Templates and how to use multiple functions for a variable in a smarty template

http://www.bkjia.com/PHPjc/813354.html www.bkjia.com true http://www.bkjia.com/PHPjc/813354.html techarticle This article shares the differences between $globals and global in the hyper-global variables in PHP. One, the Super global variable $globals PHP Super global variable has a lot, as below all belongs to the Super global variable (Su ...

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