Detailed explanation of PHP variable scope (1/3)_PHP tutorial

WBOY
Release: 2016-07-20 11:11:02
Original
863 people have browsed it

The scope of a variable is the context in which it is defined (that is, the scope in which it takes effect). 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';

?>
 代码如下 复制代码

$a = 1; /* global scope */

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

Test();
?>

Here 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:

The code is as follows Copy code

$a = 1; /* global scope */


function Test()

{
代码如下 复制代码

$a = 1;
$b = 2;

function Sum()
{
global $a, $b;

$b = $a + $b;
}

Sum();
echo $b;
?>

echo $a ; /* reference to local scope variable */

}

Test();

?>


This script There will be no output because the echo statement refers to a local version of the variable $a, and it is not assigned a value in 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.

 代码如下 复制代码

$a = 1;
$b = 2;

function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}

Sum();
echo $b;
?>

global keyword

First, an example of using global:

Example #1 Using global

The code is as follows Copy code
The above script The output will be "3". Global variables $a and $b are declared in the function, and all reference variables of any variable will point to the global variables. PHP has no limit on the maximum number of global variables that a function can declare.
The second way to access variables in the global scope is to use a special PHP custom $GLOBALS array. The previous example can be written as: Example #2 Use $GLOBALS instead of global
The code is as follows Copy code
$a = 1;<🎜>$b = 2;<🎜><🎜>function Sum()<🎜>{<🎜> $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];<🎜>}<🎜 ><🎜>Sum();<🎜>echo $b;<🎜>?>
1 2 3 http://www.bkjia.com/PHPjc/444672.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444672.htmlTechArticleThe scope of a variable is the context in which it is defined (that is, the scope in which it takes effect). Most PHP variables have only a single scope. This single range span also contains incl...
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!