Difference between global variables $ GLOBALS and global in PHP _ PHP Tutorial

Source: Internet
Author: User
Tags exit in
The difference between global variables $ GLOBALS and global in PHP. This article shares the differences between Super global variables $ GLOBALS and global in PHP. 1. there are many Super global variables $ GLOBALSPHP, and the following are all super global variables (Su this article will share the differences between Super global variables $ GLOBALS and global in PHP.

1. Super global variable $ GLOBALS
 
PHP has many Super global variables, and the following are all super global variables ):
 
$ GLOBALS, $ _ SERVER, $ _ GET, $ _ POST, $ _ FILES, $ _ COOKIE, $ _ SESSION, $ _ REQUEST, $ _ ENV.
 
Official notes:

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

A global combination array containing all the variables. The variable name is the key of the array.

You can use the $ GLOBALS array to obtain the global variables that have been generated.

In the PHP lifecycle, the so-called global variables defined outside the function body cannot be directly obtained within the function.

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

In the above example, $ foo to access the external must use the $ GLOBALS array. It is also applicable to external global variables that come in through the include file.
 
In php, global also has the following features:

Global generates an alias variable pointing to the external variable of the function in the function, instead of the real external variable of the function.

$ GLOBALS [] is actually called as an external variable, and the internal and external functions will always be consistent.
 
For member variables in the class, functions in the class must be accessed using the $ this-> method, and the $ GLOBALS method cannot be used:
 
Global is used to define global variables. However, this global variable is not applied to the entire website, but to the current page, including all files including include or require.

II. Examples

function t1(){    global $var1,$var2;    $var2=&$var1;}function t2(){    $GLOBALS['var3']=&$GLOBALS['var1'];}$var1=5;$var2=$var3=0;t1();print $var2."\n";t2();print $var3."\n";

The execution result is:

0
5

Why is one 0 and one 5 instead of two five? Modify the example as follows:

function t1(){    global $var1;    $var1=2;    unset($var1);}function t2(){    $GLOBALS['var1']=3;    unset($GLOBALS['var1']);}$var1=1;t1();print $var1."\n";t2();print $var1."\n";

Enter 2 for the execution result;

1. $ GLOBALS is an array 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. Therefore, after $ GLOBALS ['var1'] is deleted, the variable does not exist, all cannot be output.

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

2. "global $ var1;" is the alias variable "$ var1" that generates the external function $ var1. it is not a real external function variable and only exists inside the function, therefore, deleting an alias variable in a function does not affect the external variables, but you can modify the values of the external variables of the function.

Some people may want to know the difference between one and the other:

In the learning of php programs, including other programs, you can experiment by yourself. based on the results, you may think faster and more accurate than searching on the Internet. Next, let's take a look at how php accesses variables globally?

Example 1: global defines global variables.

function test_global(){    global $var1;    $var1='ok';    unset($var1);}test_global();$var2=&$var1;unset($var1);echo $var2;

Run the program without providing the result. Variables in the function can be accessed. As can be seen from the results, unset only disconnects the variable name from the variable value, and does not immediately destroy the value of the variable, and global variables defined in the function, actually, only the internal alias of the function is used externally, so we can still access $ var1 outside.

Example 2: $ GLOBALS accesses the variables defined outside the function within the function.

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

An error occurs when $ waibu is used directly in the function.

Articles you may be interested in
  • Differences between variables and functions in php after the static keyword is added
  • Php $ GLOBALS Super global variable analysis
  • Use break, continue, goto, return, and exit in multiple loops in PHP
  • Differences and usage of return, exit, break, and contiue in PHP
  • Differences between echo, print, print_r, var_export, and var_dump in php
  • Php Output Control: Understanding the differences between ob_flush and flush
  • Empty, is_null, and isset in php
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template

Bytes. 1. there are many Super global variables $ globals php Super global variables. The following are all super global variables (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.