📄 language.variables.scope.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Variable scope</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.variables.predefined.html">Predefined Variables</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.variables.variable.html">Variable variables</a></div> <div class="up"><a href="language.variables.html">Variables</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="language.variables.scope" class="sect1"> <h2 class="title">Variable scope</h2> <p class="simpara"> The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well. For example: </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />include </span><span style="color: #DD0000">'b.inc'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="simpara"> Here the <var class="varname">$a</var> variable will be available within the included <var class="filename">b.inc</var> script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. For example: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #FF8000">/* global scope */ <br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">Test</span><span style="color: #007700">()<br />{ <br /> echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">; </span><span style="color: #FF8000">/* reference to local scope variable */ <br /></span><span style="color: #007700">} <br /><br /></span><span style="color: #0000BB">Test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="simpara"> This script will not produce any output because the echo statement refers to a local version of the <var class="varname">$a</var> variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function. </p> <div id="language.variables.scope.global" class="sect2"> <h3 class="title">The global keyword</h3> <p class="simpara"> First, an example use of <i>global</i>: </p> <p class="para"> <div class="example"> <p><b>Example #1 Using global</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />function </span><span style="color: #0000BB">Sum</span><span style="color: #007700">()<br />{<br /> global </span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br /><br /> </span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />} <br /><br /></span><span style="color: #0000BB">Sum</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> The above script will output "3". By declaring <var class="varname">$a</var> and <var class="varname">$b</var> global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function. </p> <p class="simpara"> A second way to access variables from the global scope is to use the special PHP-defined <var class="varname"><a href="reserved.variables.globals.html" class="classname">$GLOBALS</a></var> array. The previous example can be rewritten as: </p> <p class="para"> <div class="example"> <p><b>Example #2 Using <var class="varname"><a href="reserved.variables.globals.html" class="classname">$GLOBALS</a></var> instead of global</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />function </span><span style="color: #0000BB">Sum</span><span style="color: #007700">()<br />{<br /> </span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'b'</span><span style="color: #007700">] = </span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'a'</span><span style="color: #007700">] + </span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'b'</span><span style="color: #007700">];<br />} <br /><br /></span><span style="color: #0000BB">Sum</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> The <var class="varname"><a href="reserved.variables.globals.html" class="classname">$GLOBALS</a></var> array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how <var class="varname"><a href="reserved.variables.globals.html" class="classname">$GLOBALS</a></var> exists in any scope, this is because $GLOBALS is a <a href="language.variables.superglobals.html" class="link">superglobal</a>. Here's an example demonstrating the power of superglobals: </p> <p class="para"> <div class="example"> <p><b>Example #3 Example demonstrating superglobals and scope</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test_global</span><span style="color: #007700">()<br />{<br /> </span><span style="color: #FF8000">// Most predefined variables aren't "super" and require <br /> // 'global' to be available to the functions local scope.<br /> </span><span style="color: #007700">global </span><span style="color: #0000BB">$HTTP_POST_VARS</span><span style="color: #007700">;<br /> <br /> echo </span><span style="color: #0000BB">$HTTP_POST_VARS</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">];<br /> <br /> </span><span style="color: #FF8000">// Superglobals are available in any scope and do <br /> // not require 'global'. Superglobals are available <br /> // as of PHP 4.1.0, and HTTP_POST_VARS is now<br /> // deemed deprecated.<br /> </span><span style="color: #007700">echo </span><span style="color: #0000BB">$_POST</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div id="language.variables.scope.static" class="sect2"> <h3 class="title">Using static variables</h3> <p class="simpara"> Another important feature of variable scoping is the <em class="emphasis">static</em> variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example: </p> <p class="para"> <div class="example"> <p><b>Example #4 Example demonstrating need for static variables</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">Test</span><span style="color: #007700">()<br />{<br /> </span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$a</span><span style="color: #007700">++;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -