One of the global and $ GLOBALS [] analyses in php

Source: Internet
Author: User

This may cause some problems. Some may carelessly change a global variable. Global variables in PHP must be declared as Global when used in functions (note that the Global keyword is useful in function definition ).
1: 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.
Copy codeThe Code is as follows:
<? PHP
$ A = 123;
Function aa ()
{
Global $ a; // If $ a is not defined as a global variable, the function body cannot access $ a outside the function body, but can be defined with the same name $, this variable is a local variable. It is equivalent to a local variable in C language and can only be used within the function body.
Echo $;
}
Aa ();
?>

Conclusion: global variables defined in the function body can be used in vitro. global variables defined in the function body cannot be used in the function body,
Copy codeThe Code is as follows:
$ Global $;
$ A = 123;
Function f ()
{
Echo $ a; // error,
}
// Take a look at the following example.
Function f ()
{
Global $;
$ A = 123;
}
F ();
Echo $ a; // correct, which can be used

2: global Problem Analysis:
Question: In config. inc. php defines some variables ($ a). In other files, the function external include ("config. inc. php "), the function needs to use these variables $ a internally. If no declaration is made, echo $ a cannot print anything. Therefore, we declare global $ a. But there are a lot of functions and many variables that cannot be repeatedly declared? Please advise if you have any good solutions.
Answer1: first define a constant in config. inc. php: define (constant name, constant value)
Then, in other places that need to be used, require 'config. inc. php ',
Then you can directly use this constant in this file.
Answer2: I also have a way to define an array, such as $ x [a], $ x, so that we only need to declare a global $ x.
Answer3: I tried your method. No.
Answer4: Modify your php. ini file.
3. Examples of Global and $ GLOBALS Arrays

Example: Use global
Copy codeThe Code is as follows:
<? PHP
$ W3sky = 1;
$ W3sky2 = 2;
Function Sum ()
{
Global $ w3sky, $ w3sky2; $ w3sky2 = $ w3sky + $ w3sky2;
} Sum ();
Echo $ w3sky2;
?>

The output of the above script is "3 ". Specify the global variables $ w3sky and $ w3sky2 in the function. All referenced variables of any variable point to the global variable. PHP has no limit on the maximum number of global variables that a function can declare.
The second way to access variables globally is to use a special PHP custom $ GLOBALS array. The preceding example can be written as follows:
Replace global with $ GLOBALS.
Copy codeThe Code is as follows:
<? PHP
$ W3sky = 1;
$ W3sky2 = 2; function Sum ()
{
$ GLOBALS ['w3sky'] = $ GLOBALS ['w3sky'] + $ GLOBALS ['w3sky2 '];
} Sum ();
Echo $ w3sky2;
?>

In the $ GLOBALS array, each variable is an element. The key name corresponds to the variable name and the value corresponds to the variable content. $ GLOBALS exists globally because $ GLOBALS is a super global variable. The following example shows how to use a super global variable:
Examples: Examples of hyperglobal variables and scopes
Copy codeThe Code is as follows:
<? PHP
Function test_global ()
{
// Most of the predefined variables are not "super". They need to use 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 range and do not require 'global' declaration. Superglobals was introduced in PHP 4.1.0.
Echo $ _ POST ['name'];
}
?>

Global means that if you declare global $ db in a file, you can reference this $ db under the Declaration.
4. I thought that both global and $ GLOBALS had the same writing style, but in actual application, we found that the difference between the two is still very big!
Let's take a look at the following example:
Copy codeThe Code is as follows:
<? Php
// Example 1
Function test_global (){
Global $ var1, $ var2;
$ Var2 = & $ var1;
}
Function test_globals (){
$ GLOBALS ['var3'] = & $ GLOBALS ['var1'];
}
$ Var1 = 5;
$ Var2 = $ var3 = 0;
Test_global ();
Print $ var2. "\ n ";
Test_globals ();
Print $ var3. "\ n ";
?>

Copy code
The execution result is:
0
5
How can this happen? Shouldn't it be 2 or 5? How can one 0 and one 5 appear?
Well, let's keep the above questions for an in-depth analysis of the principles of $ GLOBALS and global!
We all know that variables are actually the "code" in the corresponding physical code. Suppose the memory allocated by the three variables we declared above is as follows:
$ GLOBALS reference to the php manual:
Global variable: $ GLOBALS
Note: $ GLOBALS is applicable to PHP 3.0.0 and later versions.
An array composed 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.
That is to say, $ var1 and $ GLOBALS ['var1'] in the above Code refer to the same variable, rather than two different variables!
Next, let's analyze what global has done?
We all know that all the variables produced by functions in php are private variables of the function, so the variables generated by the global keyword certainly cannot escape this rule. Why do we say this? Let's look at the following code:
Copy codeThe Code is as follows:
<? Php
// Example 2
Function test (){
Global $;
Unset ($ );
}
$ A = 1;
Test ();
Print $;
?>

Copy code
The execution result is:
1
Why does output 1? Didn't $ a have been given to unset? Unset failed? Php bug?
None of them. In fact, the unset function works. It is used to drop $ a from the test function to the unset function. You can add it after the function.
Print $;
Copy code
To test! That is to say, global generates the alias variable "$ a" for the external $ a function test. to be different from the external $ a variable, I make it -- test-> $, in this case, we can draw the following figure:
After test_globals is executed, check the variable changes:
Now, we can see why $ var2 is 0 and $ var3 is 5 after Example 1 is executed!
So we come to the conclusion that the difference between global and $ GLOBALS [] in the function is:
Global generates an alias variable pointing to the external variable of the function, instead of the real external variable of the function, some unexpected situations will occur (why is the result 2 printed? In fact, it is because the $ var1 reference points to the reference address of $ var2. The actual value is not changed. At this time, only the pointer pointing to $ var1 points to the $ var2 pointer, but the pointer points to a change, but it does not actually change the value of $ var2, therefore, the value of $ var2 remains unchanged.
$ GLOBALS [] is actually called as an external variable, and the internal and external functions will always be consistent!
Note: (Go Back To Example 1 above and check the code "$ var2 = & $ var1;" in test_global. The above is a reference value assignment operation, that is, $ var2 will point to the physical memory address pointed to by var1. Therefore, after test_global function is executed in Example 1, variable changes only have an effect on the local part of the function, external function $ var2 points to the physical memory address and does not change, or it itself. (important)
Return to Example 1 above and check the code "$ var2 = & $ var1;" in test_global. The above is a reference value assignment operation, that is, $ var2 points to the physical memory address pointed to by var1, so after test_global function is executed in Example 1, the variable changes can be seen from)
This analysis is not thorough enough. If you do not understand it, please refer to Analysis 2 of global and $ GLOBALS [].

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.