The difference between global and $globals[in PHP ____php

Source: Internet
Author: User
Many people think that global and $globals[] are just the differences in writing, but they are not.

According to the official explanation,

$GLOBALS [' var '] is an external global variable $var itself.
The global $var is a reference or pointer with the same name as an external $var. (Error: An individual name reference, not a pointer ...) )

For example, please explain:


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

Test ();
echo $var 2;
?>
Normal print result is 1


<?php
$var 1 = 1;
$var 2 = 2;

function Test () {
Global $var 1, $var 2;
$var 2 = & $var 1;
echo $var 2;
$var 2 = ' snsgou.com ';
}

Test (); Output 1
echo $var 2; Output 2
echo $var 1; Output snsgou.com
?>

The $VAR1 in the test () function, $va 2 are local variables, except that the global keyword is added, referring to $var1, $va 2, when $var 2 = & $var 1; , the local variable $var2 no longer points to the global variable $val2 and points back to the global variable $var1, in other words, the change of local variable $var2 will not affect the global variable $val2, but will affect the reset global variable $VAL1.

Let's look at one more example.

<?php
$var 1 = 1;
function Test () {
unset ($GLOBALS [' var1 ']);
}
Test ();
echo $var 1;
?>
Because the $var1 was deleted, nothing was printed.


<?php
$var 1 = 1;
function Test () {
Global $var 1;
Unset ($var 1);
}

Test ();
echo $var 1;
?>
The 1 was printed unexpectedly.

The deletion is only an alias, $GLOBALS [' var '] reference, the value of its own has not been any change.

Get it.

In other words, global $var is $var = & $GLOBALS [' var ']. Call an alias of an external variable.

Discussion on +++ (II.) ++++++++++++++++++++++++++++++++++++++++++

In PHP, Global and $globals not only writing not the same thinking, the difference between the two is still very large, in practical applications need attention.

Let's look at the following example:

<?php
$id = 1;
function Test () {
Global $id;
Unset ($id);
}
Test ();
Echo ($id); Output 1
?>

Reference positioning

Many of the syntax constructs of PHP are implemented by reference mechanisms, so all of the above about reference bindings apply to these constructs as well. Some structures, such as reference passing and reference returns, have been mentioned above. Other structures that use references are:

When declaring a variable with global $var, a reference to a global variable is actually established. In other words, it is the same as doing this:

<?php
$GLOBALS ["var1"] = 1;
$var = & $GLOBALS ["Var1"];
Unset ($var);
echo $GLOBALS [' var1 ']; Output 1
//############################################
$GLOBALS ["var1"] = 1;
$var = & $GLOBALS ["Var1"];
unset ($GLOBALS [' var1 ']);
Echo $var; Output 1
//############################################
If you write as follows, you will get an error
$GLOBALS ["var"] = 1;
$var = & $GLOBALS ["var"];
unset ($GLOBALS [' var ']);
Echo $var; Script cannot execute
//###########################################
?>

This means, for example, that unset $var does not unset global variables.

Unset just breaks the binding between the variable name and the variable content. This does not mean that the variable content has been destroyed.

Returns False when using the Isset ($var). $this in the method of an object, $this is always a reference to the object that called it.

If you assign a reference to a variable declared as global within a function, the reference is visible only inside the function.

You can avoid this by using a $GLOBALS array.

Example refers to a global variable within a function:

<?php
$var 1 = "Example variable";
$var 2 = "";

function global_references ($use _globals) {
Global $var 1, $var 2;
if (! $use _globals) {
$var 2 = & $var 1; Visible only inside the function
} else {
$GLOBALS ["var2"] = & $var 1; Visible also in the global context
}
}

Global_references (FALSE);
echo "VAR2 is set to ' $var 2 '
"; VAR2 is set to ' "
Global_references (TRUE);
echo "VAR2 is set to ' $var 2 '
"; VAR2 is set to ' Example variable '
?>

$var the global; As $var = & $GLOBALS [' var ']; 's Shorthand. So if you assign other references to $var, only the references to local variables are changed.

As mentioned earlier, references are not pointers. This means that the following structure does not produce the desired effect:

<?php
$bar = 3;
function foo (& $var) {
$GLOBALS ["baz"] = 5;
$var = & $GLOBALS ["Baz"];
}
Foo ($bar);
echo $bar;//Output 3
?>

This will cause the $var variable in the Foo function to be bound to the $bar at the time of the function call, but then to be rebind to the $GLOBALS ["Baz"].

It is not possible to bind a $bar to another variable in the scope of a function call through a reference mechanism, because there is no variable $bar in the function foo (it is represented as $var, but $var only the variable content but not the binding of the name to the value in the symbol table). You can use reference returns to refer to variables that are selected by the function.

The $globals explanation for quoting the PHP manual:

Global variables: $GLOBALS, note: $GLOBALS are available in PHP 3.0.0 and later versions.

An array of all defined global variables. The variable name is the index of the array. This is a "superglobal", or it can be described as an automatic global variable.

In other words, the $var1 and $globals[' var1 ' in the above code refer to the same variable, not 2 different variables.

If you assign a reference to a variable declared as global within a function, the reference is visible only inside the function. You can avoid this by using a $GLOBALS array.

We all know that the variables generated by the functions in PHP are the private variables of the function, the global keyword generates a variable that cannot escape this rule, and global creates an alias variable in the function that points to the external variable of the function, rather than the actual function external variable, but changes the point address of the alias variable. Something unexpected happens, $GLOBALS [] Indeed the actual call is an external variable, and the function is always consistent inside and outside.

<?php
$a = 1;
$b = 2;
function Sum () {
Global $a, $b;
$b = $a + $b;
}
Sum ();
Echo $b;
?>

The output will be "3″." The global variable $a and $b are declared in the function, and all reference variables of any variable are pointed to the global variable.

How is not 2, outside the function does not affect, please note that $b in the function is not modified by reference, but modified $b point to the physical memory value, so the external input is 3.

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.