PHP Global variable definition usage

Source: Internet
Author: User

Php parses global variables as follows: Global defines global variables, but this Global variable is not applied to the entire website, but to the current page, including all files of include or require.


Example

The Code is as follows: Copy code

<? Php
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B; // declare it as a global variable
$ B = $ a + $ B;
}
Sum ();
Echo $ B;
?>

Result: 3
If no global variable is available, the values $ a and $ B cannot be obtained in the method. Therefore, to use an external variable in the method, you must declare this variable as a global variable first, in this way, you can use

Conclusion: PHP 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,

$ Glpbal $ a; $ a = 123; function f () {echo $ a; // error ,}

Let's take a look at the following example:

The Code is as follows: Copy code

Function f ()
{
Global $;
$ A = 123;
}
F ();
Echo $ a; // correct, which can be used

In a user-defined function, a local function range is introduced. Any variables used in the function will be limited to the local function scope by default (including the variables in the include and require imported files )!
Explanation:. test_Global in the PHP file is a defined third-party function, which is imported into B using include. the global variable of $ a in the PHP file, so $ a is restricted to the range of local functions of Test_Global, So B. $ a in the PHP file takes effect in Test_Global, instead of. php ....
Solution:
1. rush out of the local Function
// A. php file

The Code is as follows: Copy code

<? Php
Function Test_Global ()
{
Test ();
}
Include 'B. php'; // remove include from the local Test_Global Function
$ A = 0;
Test_Global ();
Echo $;
?>
// B. php file
<? Php
Function Test ()
{
Global $;
$ A = 1;
}
?>

2. Excellent accessors

The Code is as follows: Copy code

// A. php file
<? Php
Include 'B. php ';
$ A = 0;
Set_Global ($ );
Echo $;
?>
// B. php file
<? Php
Function Set_Global (& $ var)
{
$ Var = 1;
}
?>

Let's take a look at $ GLOBALS [].

The Code is as follows: Copy code


$ Var1 = 1;
$ Var2 = 2;
Function test1 (){
$ GLOBALS ['var2'] = & $ GLOBALS ['var1'];
}
Test1 ();
Echo $ var2; // 1

$ Var3 = 1;
$ Var4 = 2;
Function test2 (){
Global $ var3, $ var4;
$ Var4 = & $ var3;
}
Test2 ();
Echo $ var4;

// 2 Why is the printing result of $ var2 1 and that of $ var4 2? In fact, it is because the reference of $ var3 points to the reference address of $ var4. The actual value of $ var4 is not changed. The official explanation is: $ GLOBALS ['var'] is the external global variable itself, and global $ var is the reference or pointer of the external $ var with the same name.

Maybe this example is not very clear. I will introduce another example:

The Code is as follows: Copy code


$ Var1 = 1;
Function test1 (){
Unset ($ GLOBALS ['var1']);
}
Test1 ();
Var_dump ($ var1); // NULL

$ Var2 = 1;
Function test2 (){
Global $ var2;
Unset ($ var2 );
}
Test2 ();

Echo $ var2; // The value of 1 $ var1 is deleted, while the value of $ var2 still exists. This proves that $ var2 is just an alias reference and its value has not been changed. That is to say, global $ var is actually $ var = & $ GLOBALS ['var'], calling an alias for an external variable!

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.