Global explanation of global variables in PHP

Source: Internet
Author: User

This article introduces the global method of global variables in PHP in detail. If you need to know how to use global functions, refer to this article.

The scope of a variable is the context defined by it (that is, its effective range ). Most PHP variables have only one separate range. This separate range span also contains the files introduced by include and require. For example:

The Code is as follows: Copy code

<? Php
$ A = 1;
Include 'B. inc ';
?>

The variable $ a will take effect in the included file B. inc. However, in user-defined functions, a local function range will be introduced. By default, any variable used in a function is limited to a local function. For example:

The Code is as follows: Copy code

<? Php
$ A = 1;/* global scope */

Function Test ()
{
Echo $ a;/* reference to local scope variable */
}

Test ();
?>

This script does not have any output, because the echo statement references a local version of variable $ a and is not assigned a value within this range. You may notice that the global variables in PHP are a little different from those in C language. In C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. Some may accidentally change a global variable. Global variables in PHP must be declared as global when used in functions.

Today we have encountered the problem that php global variables do not work.

First, a simple code:

The Code is as follows: Copy code
<? Php
$ A = 0;
Function Test ()
{
$ A = 1;
}
Test ();
Echo $;
?>

The output in the above Code is 0, because the $ a variable in the Test function is set as a local variable by default, and the $ a scope is in the Test. Modify the Code as follows:

The Code is as follows: Copy code
<? Php
$ A = 0;
Function Test ()
{
& Nbsp; global $ a; // declare that the $ a variable used in the Test function is a global variable.
& Nbsp; $ a = 1;
}
Test ();
Echo $;
?>

After the $ a variable used in the Test function is declared as a global variable, the output of $ a is 1.
The above example is just a basic knowledge of global variables. Let's look at the complex points:

// A. php file

The Code is as follows: Copy code

<? Php
Function Test_Global ()
{& Nbsp;
Include 'B. php'; & nbsp;
Test (); & nbsp;
} & Nbsp;

$ A = 0;
Test_Global ();
Echo $;
?> & Nbsp;

// B. php file

<? Php
Function Test ()
{
& Nbsp; global $ a; // declare that the $ a variable used in the Sum function is a global variable.
& Nbsp; $ a = 1;
}
?>

Why is the output 0 ?!!

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

The Code is as follows: Copy code

// A. php file

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

// 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'; & nbsp;
$ A = 0;
Set_Global ($ );
Echo $;
?> & Nbsp;

// B. php file

<? Php
Function Set_Global (& $ var)
{
$ Var = 1;
}
?>

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.