⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.variables.scope.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />include&nbsp;</span><span style="color: #DD0000">'b.inc'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">/*&nbsp;global&nbsp;scope&nbsp;*/&nbsp;<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">Test</span><span style="color: #007700">()<br />{&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">/*&nbsp;reference&nbsp;to&nbsp;local&nbsp;scope&nbsp;variable&nbsp;*/&nbsp;<br /></span><span style="color: #007700">}&nbsp;<br /><br /></span><span style="color: #0000BB">Test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</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">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">Sum</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br />}&nbsp;<br /><br /></span><span style="color: #0000BB">Sum</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>      </div>     </div>    </p>   <p class="simpara">    The above script will output &quot;3&quot;.  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">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">Sum</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'b'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'a'</span><span style="color: #007700">]&nbsp;+&nbsp;</span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">'b'</span><span style="color: #007700">];<br />}&nbsp;<br /><br /></span><span style="color: #0000BB">Sum</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</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&#039;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">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">test_global</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Most&nbsp;predefined&nbsp;variables&nbsp;aren't&nbsp;"super"&nbsp;and&nbsp;require&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;'global'&nbsp;to&nbsp;be&nbsp;available&nbsp;to&nbsp;the&nbsp;functions&nbsp;local&nbsp;scope.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">global&nbsp;</span><span style="color: #0000BB">$HTTP_POST_VARS</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Superglobals&nbsp;are&nbsp;available&nbsp;in&nbsp;any&nbsp;scope&nbsp;and&nbsp;do&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;not&nbsp;require&nbsp;'global'.&nbsp;Superglobals&nbsp;are&nbsp;available&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;as&nbsp;of&nbsp;PHP&nbsp;4.1.0,&nbsp;and&nbsp;HTTP_POST_VARS&nbsp;is&nbsp;now<br />&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;deemed&nbsp;deprecated.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</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">?&gt;</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">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">Test</span><span style="color: #007700">()<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">++;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>     </div>    </div>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -