PHP in the global and $globals[] analysis of one of the _php skills

Source: Internet
Author: User
This can cause some problems, and some people may inadvertently change a global variable. Global variables in PHP must be declared as global when used in functions (note that the Global keyword is defined in the function to be useful).
The role of 1:global is to define global variables, but this global variable is not applied to the entire Web site, but is applied to the current page, including all files with include or require.
Copy Code code as follows:

? Php
$a = 123;
function AA ()
{
Global $a; If the $a is not defined as a global variable, the body of the function cannot access the $a of the function body, but can define an identical name $a, at this time the variable is a local variable, equivalent to the local variable of the C language, and can only be used within the function body.
echo $a;
}
AA ();
?>

Summary: Global variables defined in the function body can be used outside the function body, and global variables defined outside the function body cannot be used in the function body.
Copy Code code as follows:

$global $a;
$a = 123;
function f ()
{
echo $a; Error
}
Take another look at the following example
function f ()
{
Global $a;
$a = 123;
}
f ();
echo $a; Correct, you can use

2:global Problem Resolution:
Question: I've defined some variables ($a) in config.inc.php, in other files, outside of the function include ("config.inc.php"), the function needs to use these variables inside $a, if not declared, echo $ A is not a print out of anything. So declare global $a, but there are a lot of functions and many variables, you can't keep repeating that statement? If there is any good solution, please advise.
Answer1: First define constants in config.inc.php: Define (constant name, constant value)
and require ' config.inc.php ' in other places that need to be used,
Then you can use this constant directly in this file.
Answer2: I also have a way to define an array, such as $x[a, $x, so just declare that global $x one.
Answer3: I tried this method of yours, I can't.
Answer4: Change your php.ini file.
3. Some examples of global and $globals arrays

Example: Using Global
Copy Code code as follows:

? Php
$w 3sky = 1;
$w 3sky2 = 2;
function Sum ()
{
Global $w 3sky, $w 3sky2 $w 3sky2 = $w 3sky + $w 3sky2;
}sum ();
echo $w 3sky2;
?>

The output from the above script will be "3". The global variable $w 3sky and $w 3sky2 are declared in the function, and all reference variables of any variable are pointed to global variables. PHP has no restrictions on the maximum number of global variables that a function can declare.
The second way to access a variable globally is to use a special PHP custom $GLOBALS array. The preceding example can be written as:
Example uses $GLOBALS instead of global
Copy Code code as follows:

? Php
$w 3sky = 1;
$w 3sky2 = 2;function Sum ()
{
$GLOBALS [' W3sky '] = $GLOBALS [' W3sky '] + $GLOBALS [' w3sky2 '];
}sum ();
echo $w 3sky2;
?>

In the $GLOBALS array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the contents of the variable. $GLOBALS exists globally because $GLOBALS is a super global variable. The following example shows the usefulness of a super global variable:
Example shows examples of hyper-global variables and scopes
Copy Code code as follows:

? Php
function Test_global ()
{
Most predefined variables are not "super", and they need the ' global ' keyword to make them valid in the local area of the function.
Global $HTTP _post_vars;echo $HTTP _post_vars[' name '];//superglobals are valid in any scope and do not require ' global ' declarations. Superglobals was introduced in the PHP 4.1.0.
echo $_post[' name '];
}
?>

Global means that in a file as long as you declare it as global $db then you can quote the $DB under the statement.
4. Originally thought that global and $globals in addition to writing different thought, the other are the same, but in the actual application found that 2 of the difference is still very large!
Let's look at the following example:
Copy Code code as follows:

<?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 ";
?>

Copy Code
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" in the corresponding physical codes, assuming that the 3 variables we declared above are allocated memory as shown in the following figure:
The $globals explanation for quoting the PHP manual:
Global variables: $GLOBALS
Note: $GLOBALS is 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!
What does global have to do to analyze the following?
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:
Copy Code code as follows:

<?php
Example 2
function Test () {
Global $a;
unset ($a);
}
$a = 1;
Test ();
Print $a;
?>

Copy Code
The results of the execution are:
1
Why would you output 1? Have you already given $a to unset? Unset a failure? A bug in PHP?
is not, in fact, unset work, is the test function of the $a to unset off, you can add after the function
Print $a;
Copy Code
To test! That is, global produces the alias variable "$a" for the external $a of the test function, and to differentiate it from the outside $a, I make it--test-> $a, so the example 1 is named, and the following figure can be drawn:
And after the test_globals has been executed, look at the change of variables:
At this point, look at the picture, you can understand why example 1 after 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 (why does the print result be 2?). In fact, it's because $var1 's reference points to the $VAR2 's reference address. Causes the value of the substance to not change. At this point the pointer to the $VAR1 is pointing to the $VAR2 pointer, but the pointer is changed, but essentially there is no change to the $VAR2 value, so the $VAR2 value will remain unchanged, for example 1.
$GLOBALS [] The real call is an external variable, and the function is always consistent inside and outside!
Note: (Then go back to Example 1 above, 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 has been executed Test_ After the global function, the variable changes only in the local effect of the function, the external $var2 of the function point to the physical memory address does not change, or its own. (emphasis)
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 are shown in the following diagram
This analysis is not clear enough, do not understand the "global and $globals[] analysis of the second" which extrapolate easy to understand

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.