In the PHP language Global and $globals[] ... ____php

Source: Internet
Author: User
Tags manual php language
The second analysis of global and $globals[in PHP language

Or to borrow an example from the previous article:

PHP code
<?php
Example 1
function Test_global () {
Global $var 1, $var 2;
$var 2 =& $var 1;
}
function Test_globals () {
$GLOBALS [' Var3 '] =& $GLOBALS [' var1 '];
}
$var 1 = 5;
$var 2 = $var 3 = 0;
Test_global ();
Print $var 2. " \ n ";
Test_globals ();
Print $var 3. " \ n ";
?>
The results of the execution are:
0
5
How could it be? Shouldn't it be 2 or 5? How come there are 1 0 and a 5?



Well, we reserve the above questions and deeply analyze the principles of $globals and global!
We all know that variables are actually "code names" in the corresponding physical codes.
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.
Let's analyze what global is doing.
Reference to the PHP manual's global Explanation:
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, so the variables generated by the Global keyword will certainly not escape this rule, why do you say this, look at the following code:
PHP code
<?php
Example 2
function Test () {
Global $a;
unset ($a);
}
$a = 1;
Test ();
Print $a;
?>
The results of the execution are:
1
Why would you output 1? Have you already given $a to unset? Unset has failed. A bug in PHP.
No, in fact, unset is working, is the test function of the $a to unset off, you can add in the function test ()
Print $a;
To test.
Then go back to Example 1 above, and look at this code in Test_global "$var 2 =& $var 1;" Above is a reference assignment operation, where $VAR2 will point to the physical memory address that var1 points to, so example 1 after the Test_global function is executed , the variable changes only in the function of the local effect, the function outside the $var2 point to the physical memory address does not change, or its own.
At this point, you can understand why example 1 after the execution, $var 2 is 0, and $VAR3 is 5.
So we come to the conclusion that the difference between global and $globals[in a function is:
Global creates an alias variable in the function that points to the external variable of the function, rather than a real function external variable, but when the address of the alias variable is changed, something unexpected happens, such as Example 1.
$GLOBALS [] Indeed the actual call is an external variable, and the function is always consistent inside and outside the
The impression can be deepened against the following two columns:
Global
<?php
function MyFunction () {
Global $bar;
Unset ($bar);
}
$bar = "someting";
MyFunction ();
Echo $bar;
?>
Output: someting
$GLOBALS []:
<?php
function foo ()
{
unset ($GLOBALS [' Bar ']);
}
$bar = "something";
Foo ();
Echo $bar;
?>
Output: Empty
When according to the above understanding of the idea, encountered the following situation is not a little dizzy it?
<?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.

Related Article

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.