<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Variable scope</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Variables" HREF="language.variables.html"><LINK REL="PREVIOUS" TITLE="Predefined variables" HREF="language.variables.predefined.html"><LINK REL="NEXT" TITLE="Variable variables" HREF="language.variables.variable.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-2"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.variables.predefined.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Cap. 7. Variables</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.variables.variable.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.variables.scope" >Variable scope</A ></H1 ><P > 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" ><P ></P ><A NAME="AEN1555" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$a </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br />include </font><font color="#DD0000">"b.inc"</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > Here the <VAR CLASS="varname" >$a</VAR > variable will be available within the included <TT CLASS="filename" >b.inc</TT > 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" ><P ></P ><A NAME="AEN1560" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$a </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">; </font><font color="#FF8000">/* global scope */ <br /><br /></font><font color="#007700">function </font><font color="#0000BB">Test</font><font color="#007700">()<br />{ <br /> echo </font><font color="#0000BB">$a</font><font color="#007700">; </font><font color="#FF8000">/* reference to local scope variable */ <br /></font><font color="#007700">} <br /><br /></font><font color="#0000BB">Test</font><font color="#007700">();<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > 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 CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.variables.scope.global" >The global keyword</A ></H2 ><P > First, an example use of <VAR CLASS="literal" >global</VAR >: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1569" ></A ><P ><B >Exemplu 7-1. Using global</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$a </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br /></font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">2</font><font color="#007700">;<br /><br />function </font><font color="#0000BB">Sum</font><font color="#007700">()<br />{<br /> global </font><font color="#0000BB">$a</font><font color="#007700">, </font><font color="#0000BB">$b</font><font color="#007700">;<br /><br /> </font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">$a </font><font color="#007700">+ </font><font color="#0000BB">$b</font><font color="#007700">;<br />} <br /><br /></font><font color="#0000BB">Sum</font><font color="#007700">();<br />echo </font><font color="#0000BB">$b</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > 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 > A second way to access variables from the global scope is to use the special PHP-defined <VAR CLASS="varname" >$GLOBALS</VAR > array. The previous example can be rewritten as: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1578" ></A ><P ><B >Exemplu 7-2. Using <VAR CLASS="varname" >$GLOBALS</VAR > instead of global</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$a </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">;<br /></font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">2</font><font color="#007700">;<br /><br />function </font><font color="#0000BB">Sum</font><font color="#007700">()<br />{<br /> </font><font color="#0000BB">$GLOBALS</font><font color="#007700">[</font><font color="#DD0000">"b"</font><font color="#007700">] = </font><font color="#0000BB">$GLOBALS</font><font color="#007700">[</font><font color="#DD0000">"a"</font><font color="#007700">] + </font><font color="#0000BB">$GLOBALS</font><font color="#007700">[</font><font color="#DD0000">"b"</font><font color="#007700">];<br />} <br /><br /></font><font color="#0000BB">Sum</font><font color="#007700">();<br />echo </font><font color="#0000BB">$b</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > The <VAR CLASS="varname" >$GLOBALS</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" >$GLOBALS</VAR > exists in any scope, this is because $GLOBALS is a <A HREF="language.variables.predefined.html#language.variables.superglobals" >superglobal</A >. Here's an example demonstrating the power of superglobals: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1587" ></A ><P ><B >Exemplu 7-3. Example demonstrating superglobals and scope</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">test_global</font><font color="#007700">()<br />{<br /> </font><font color="#FF8000">// Most predefined variables aren't "super" and require <br /> // 'global' to be available to the functions local scope.<br /> </font><font color="#007700">global </font><font color="#0000BB">$HTTP_POST_VARS</font><font color="#007700">;<br /> <br /> echo </font><font color="#0000BB">$HTTP_POST_VARS</font><font color="#007700">[</font><font color="#DD0000">'name'</font><font color="#007700">];<br /> <br /> </font><font color="#FF8000">// Superglobals are available in any scope and do <br /> // not require 'global'. Superglobals are available <br /> // as of PHP 4.1.0<br /> </font><font color="#007700">echo </font><font color="#0000BB">$_POST</font><font color="#007700">[</font><font color="#DD0000">'name'</font><font color="#007700">];<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.variables.scope.static" >Using static variables</A ></H2 ><P > Another important feature of variable scoping is the <SPAN CLASS="emphasis" ><I CLASS="emphasis" >static</I ></SPAN > 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 > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1595" ></A ><P ><B >Exemplu 7-4. Example demonstrating need for static variables</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">Test </font><font color="#007700">()<br />{<br /> </font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br /> echo </font><font color="#0000BB">$a</font><font color="#007700">;<br /> </font><font color="#0000BB">$a</font><font color="#007700">++;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > This function is quite useless since every time it is called it sets <VAR CLASS="varname" >$a</VAR > to <VAR CLASS="literal" >0</VAR > and prints "0". The <VAR CLASS="varname" >$a</VAR >++ which increments the variable serves no purpose since as soon as the function exits the <VAR CLASS="varname" >$a</VAR > variable disappears. To make a useful counting function which will not lose track of the current count, the <VAR CLASS="varname" >$a</VAR > variable is declared static: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1605" ></A ><P ><B >Exemplu 7-5. Example use of static variables</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">Test</font><font color="#007700">()<br />{<br /> static </font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br /> echo </font><font color="#0000BB">$a</font><font color="#007700">;<br /> </font><font color="#0000BB">$a</font><font color="#007700">++;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Now, every time the Test() function is called it will print the value of <VAR CLASS="varname" >$a</VAR > and increment it. </P ><P > Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable <VAR CLASS="varname" >$count</VAR > to know when to stop: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1613" ></A ><P ><B >Exemplu 7-6. Static variables with recursive functions</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">Test</font><font color="#007700">()<br />{<br /> static </font><font color="#0000BB">$count </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br /><br /> </font><font color="#0000BB">$count</font><font color="#007700">++;<br /> echo </font><font color="#0000BB">$count</font><font color="#007700">;<br /> if (</font><font color="#0000BB">$count </font><font color="#007700">< </font><font color="#0000BB">10</font><font color="#007700">) {<br /> </font><font color="#0000BB">Test </font><font color="#007700">();<br /> }<br /> </font><font color="#0000BB">$count</font><font color="#007700">--;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > Static variables maybe declared as seen in the examples above. Trying to assign values to these variables which are the result of expressions will cause a parse error. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN1619" ></A ><P ><B >Exemplu 7-7. Declaring static variables</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">foo</font><font color="#007700">(){<br /> static </font><font color="#0000BB">$int </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">; </font><font color="#FF8000">// correct <br /> </font><font color="#007700">static </font><font color="#0000BB">$int </font><font color="#007700">= </font><font color="#0000BB">1</font><font color="#007700">+</font><font color="#0000BB">2</font><font color="#007700">; </font><font color="#FF8000">// wrong (as it is an expression)<br /> </font><font color="#007700">static </font><font color="#0000BB">$int </font><font color="#007700">= </font><font color="#0000BB">sqrt</font><font color="#007700">(</font><font color="#0000BB">121</font><font color="#007700">); </font><font color="#FF8000">// wrong (as it is an expression too)<br /><br /> </font><font color="#0000BB">$int</font><font color="#007700">++;<br /> echo </font><font color="#0000BB">$int</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.variables.scope.references" >References with global and static variables</A ></H2 ><P > The Zend Engine 1, driving <VAR CLASS="literal" >PHP 4</VAR >, implements the <A HREF="language.variables.scope.html#language.variables.scope.static" >static</A > and <A HREF="language.variables.scope.html#language.variables.scope.global" >global</A > modifier for variables in terms of <A HREF="language.references.html" > references</A >. For example, a true global variable imported inside a function scope with the <VAR CLASS="literal" >global</VAR > statement actually creates a reference to the global variable. This can lead to unexpected behaviour which the following example addresses: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN1630" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function </font><font color="#0000BB">test_global_ref</font><font color="#007700">() {<br /> global </font><font color="#0000BB">$obj</font><font color="#007700">;<br /> </font><font color="#0000BB">$obj </font><font color="#007700">= &new </font><font color="#0000BB">stdclass</font><font color="#007700">;<br />}<br /><br />function </font><font color="#0000BB">test_global_noref</font><font color="#007700">() {<br /> global </font><font color="#0000BB">$obj</font><font color="#007700">;<br /> </font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">stdclass</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">test_global_ref</font><font color="#007700">();<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">);<br /></font><font color="#0000BB">test_global_noref</font><font color="#007700">();<br /></font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > Executing this example will result in the following output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >NULL object(stdClass)(0) { }</PRE ></TD ></TR ></TABLE ><P > A similar behaviour applies to the <VAR CLASS="literal" >static</VAR > statement. References are not stored statically: </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN1636" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">function &</font><font color="#0000BB">get_instance_ref</font><font color="#007700">() {<br /> static </font><font color="#0000BB">$obj</font><font color="#007700">;<br /><br /> echo </font><font color="#DD0000">"Static object: "</font><font color="#007700">;<br /> </font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">);<br /> if (!isset(</font><font color="#0000BB">$obj</font><font color="#007700">)) {<br /> </font><font color="#FF8000">// Assign a reference to the static variable<br /> </font><font color="#0000BB">$obj </font><font color="#007700">= &new </font><font color="#0000BB">stdclass</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">$obj</font><font color="#007700">-></font><font color="#0000BB">property</font><font color="#007700">++;<br /> return </font><font color="#0000BB">$obj</font><font color="#007700">;<br />}<br /><br />function &</font><font color="#0000BB">get_instance_noref</font><font color="#007700">() {<br /> static </font><font color="#0000BB">$obj</font><font color="#007700">;<br /><br /> echo </font><font color="#DD0000">"Static object: "</font><font color="#007700">;<br /> </font><font color="#0000BB">var_dump</font><font color="#007700">(</font><font color="#0000BB">$obj</font><font color="#007700">);<br /> if (!isset(</font><font color="#0000BB">$obj</font><font color="#007700">)) {<br /> </font><font color="#FF8000">// Assign the object to the static variable<br /> </font><font color="#0000BB">$obj </font><font color="#007700">= new </font><font color="#0000BB">stdclass</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">$obj</font><font color="#007700">-></font><font color="#0000BB">property</font><font color="#007700">++;<br /> return </font><font color="#0000BB">$obj</font><font color="#007700">;<br />}<br /><br /></font><font color="#0000BB">$obj1 </font><font color="#007700">= </font><font color="#0000BB">get_instance_ref</font><font color="#007700">();<br /></font><font color="#0000BB">$still_obj1 </font><font color="#007700">= </font><font color="#0000BB">get_instance_ref</font><font color="#007700">();<br />echo </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#0000BB">$obj2 </font><font color="#007700">= </font><font color="#0000BB">get_instance_noref</font><font color="#007700">();<br /></font><font color="#0000BB">$still_obj2 </font><font color="#007700">= </font><font color="#0000BB">get_instance_noref</font><font color="#007700">();<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > Executing this example will result in the following output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >Static object: NULL Static object: NULL Static object: NULL Static object: object(stdClass)(1) { ["property"]=> int(1) }</PRE ></TD ></TR ></TABLE ><P > This example demonstrates that when assigning a reference to a static variable, it's not <SPAN CLASS="emphasis" ><I CLASS="emphasis" >remembered</I ></SPAN > when you call the <VAR CLASS="literal" >&get_instance_ref()</VAR > function a second time. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.variables.predefined.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.variables.variable.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Predefined variables</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.variables.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Variable variables</TD ></TR ></TABLE ></DIV ></BODY ></HTML >