Home  >  Article  >  Backend Development  >  Detailed explanation of global variables in PHP_PHP tutorial

Detailed explanation of global variables in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:581004browse

This article will introduce in detail the method of global variable global in PHP. Friends who need to know how to use the global function can refer to this article.

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

The code is as follows Copy code
 代码如下 复制代码

$a = 1;
include 'b.inc';
?>

$a = 1;
include 'b.inc';
?>

This variable $a will take effect in the included file b.inc. However, in user-defined functions, a local function scope will be introduced. Any variables used inside a function will be restricted to the local function scope by default. For example:
 代码如下 复制代码

$a = 1; /* global scope */

function Test()
{
echo $a; /* reference to local scope variable */
}

Test();
?>

The code is as follows Copy code

$a = 1; /* global scope */

function Test()
{
echo $a; /* reference to local scope variable */
}

Test();
?>

 代码如下 复制代码
$a = 0 ;
function Test()
{
$a =1;
}
Test();
echo $a;
?>
This script will produce no output because the echo statement refers to a local version of the variable $a, and it is not assigned a value within this scope. You may notice that PHP's global variables are a little different from C language. In C language, global variables automatically take effect in functions unless overridden by local variables. This may cause some problems, someone may accidentally change a global variable. Global variables in PHP must be declared global when used in functions.

Today I encountered the problem that php global variables do not work.
 代码如下 复制代码
$a = 0 ;
function Test()
{
global $a;//申明函数体Test内使用的$a变量为global全局变量
$a =1;
}
Test();
echo $a;
?>
First a simple piece of code: The output in the above code is 0, that is because the $a variable in the function body Test is set to a local variable by default, and the scope of $a is within Test. Modify the code as follows

After declaring the $a variable used in the function body Test as a global global variable, $a has a global effect, so the output is 1.
The above examples are just basic knowledge of global variables. Let’s look at more complicated ones:

//A.php file

The code is as follows
 代码如下 复制代码

function Test_Global()
{
include 'B.php';
Test();
}

$a = 0 ;
Test_Global();
echo $a;
?>

//B.php 文件

function Test()
{
global $a;//申明函数体Sum内使用的$a变量为global全局变量
$a =1;
}
?>

Copy code

function Test_Global()

{

Include 'B.php';

Test();

}

代码如下 复制代码

//A.php 文件

function Test_Global()
{
Test();
}
include 'B.php'; //将include 从局部Test_Global函数中移出
$a = 0 ;
Test_Global();
echo $a;
?>

//B.php 文件

function Test()
{
global $a;
$a =1;
}
?>

$a = 0 ;

Test_Global();

echo $a;
 代码如下 复制代码

//A.php 文件
include 'B.php';
$a =0;
Set_Global($a);
echo $a;
?>

//B.php 文件

function Set_Global(&$var)
{
$var=1;
}
?>

?>  //B.php file function Test()
{ $a =1; } ?> Why is the output 0?!! In user-defined functions, a local function scope will be introduced. Any variables used inside a function will be limited to the local function scope by default (including variables in files imported by include and require)! Explanation: Test_Global in the A.php file is a defined third-party function. This function uses include to import the global global variable of $a in the B.php file, so $a is limited to the Test_Global local function scope, so B The scope of $a in the .php file is within Test_Global, instead of affecting the entire A.php.... Solution:
1. Break out of local function
The code is as follows Copy code
//A.php file function Test_Global()<🎜> { <🎜> Test(); <🎜> } <🎜> include 'B.php'; //Move include from the local Test_Global function<🎜> $a = 0 ;<🎜> Test_Global();<🎜> echo $a;<🎜> ?>  //B.php file function Test()<🎜> {<🎜> global $a;<🎜> $a =1;<🎜> }<🎜> ?> 2. Excellent accessor
The code is as follows Copy code
//A.php file include 'B.php'; <🎜> $a =0;<🎜> Set_Global($a);<🎜> echo $a;<🎜> ?>  //B.php file function Set_Global(&$var)<🎜> {<🎜> $var=1;<🎜> }<🎜> ?> http://www.bkjia.com/PHPjc/628915.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628915.htmlTechArticleThis article will introduce in detail the method of global variable global in PHP. Friends who need to know how to use the global function You can refer to this article. The scope of a variable is the context in which it is defined...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn