Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 3016

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>Variable scope</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
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-1"></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"
>PHP Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.variables.predefined.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 8. Variables</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.variables.variable.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="language.variables.scope"
></A
>Variable scope</H1
><P
>&#13;    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"
><A
NAME="AEN4093"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$a = 1;
include "b.inc";
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    Here the <TT
CLASS="varname"
>$a</TT
> 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"
><A
NAME="AEN4098"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$a = 1; /* global scope */ 

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

Test();
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    This script will not produce any output because the echo statement
    refers to a local version of the <TT
CLASS="varname"
>$a</TT
> 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.  An example:
   </P
><DIV
CLASS="informalexample"
><A
NAME="AEN4102"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$a = 1;
$b = 2;

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

    $b = $a + $b;
} 

Sum();
echo $b;
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    The above script will output "3".  By declaring
    <TT
CLASS="varname"
>$a</TT
> and <TT
CLASS="varname"
>$b</TT
> 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
>&#13;    A second way to access variables from the global scope is to use
    the special PHP-defined <TT
CLASS="varname"
>$GLOBALS</TT
> array.  The
    previous example can be rewritten as:
   </P
><DIV
CLASS="informalexample"
><A
NAME="AEN4109"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
$a = 1;
$b = 2;

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

Sum();
echo $b;
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    The <TT
CLASS="varname"
>$GLOBALS</TT
> 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 <TT
CLASS="varname"
>$GLOBALS</TT
> 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
>&#13;   
   <DIV
CLASS="informalexample"
><A
NAME="AEN4116"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function test_global()
{
    // Most predefined variables aren't "super" and require 
    // 'global' to be available to the functions local scope.
    global $HTTP_POST_VARS;
    
    print $HTTP_POST_VARS['name'];
    
    // Superglobals are available in any scope and do 
    // not require 'global'.  Superglobals are available 
    // as of PHP 4.1.0
    print $_POST['name'];
}
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
   </P
><P
>&#13;    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
><DIV
CLASS="informalexample"
><A
NAME="AEN4120"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function Test ()
{
    $a = 0;
    echo $a;
    $a++;
}
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    This function is quite useless since every time it is called it
    sets <TT
CLASS="varname"
>$a</TT
> to <TT
CLASS="literal"
>0</TT
> and prints
    "0".  The <TT
CLASS="varname"
>$a</TT
>++ which increments the
    variable serves no purpose since as soon as the function exits the
    <TT
CLASS="varname"
>$a</TT
> variable disappears.  To make a useful
    counting function which will not lose track of the current count,
    the <TT
CLASS="varname"
>$a</TT
> variable is declared static:
   </P
><DIV
CLASS="informalexample"
><A
NAME="AEN4128"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function Test()
{
    static $a = 0;
    echo $a;
    $a++;
}
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    Now, every time the Test() function is called it will print the
    value of <TT
CLASS="varname"
>$a</TT
> and increment it.
   </P
><P
>&#13;    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 <TT
CLASS="varname"
>$count</TT
> to know when to stop:
   </P
><DIV
CLASS="informalexample"
><A
NAME="AEN4134"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function Test()
{
    static $count = 0;

    $count++;
    echo $count;
    if ($count &#60; 10) {
        Test ();
    }
    $count--;
}
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    The Zend Engine 1, driving <TT
CLASS="literal"
>PHP4</TT
>, implements the
    <TT
CLASS="literal"
>static</TT
> and <TT
CLASS="literal"
>global</TT
> modifier for
    variables in terms of references. For example, a true global variable
    imported inside a function scope with the <TT
CLASS="literal"
>global</TT
>
    statement actually creates a reference to the global variable. This can
    lead to unexpected behaviour which the following example addresses:
   </P
><DIV
CLASS="informalexample"
><A
NAME="AEN4141"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function test_global_ref() {
    global $obj;
    $obj = &#38;new stdclass;
}

function test_global_noref() {
    global $obj;
    $obj = new stdclass;
}

test_global_ref();
var_dump($obj);
test_global_noref();
var_dump($obj);
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    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
>&#13;    A similar behaviour applies to the <TT
CLASS="literal"
>static</TT
> statement.
    References are not stored statically:
   </P
><DIV
CLASS="informalexample"
><A
NAME="AEN4147"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php
function &#38;get_instance_ref() {
    static $obj;

    echo "Static object: ";
    var_dump($obj);
    if (!isset($obj)) {
        // Assign a reference to the static variable
        $obj = &#38;new stdclass;
    }
    $obj-&#62;property++;
    return $obj;
}

function &#38;get_instance_noref() {
    static $obj;

    echo "Static object: ";
    var_dump($obj);
    if (!isset($obj)) {
        // Assign the object to the static variable
        $obj = new stdclass;
    }
    $obj-&#62;property++;
    return $obj;
}

$obj1 = get_instance_ref();
$still_obj1 = get_instance_ref();
echo "\n";
$obj2 = get_instance_noref();
$still_obj2 = get_instance_noref();
?&#62;</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    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"]=&#62;
  int(1)
}</PRE
></TD
></TR
></TABLE
><P
>&#13;    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
    <TT
CLASS="literal"
>&#38;get_instance_ref()</TT
> function a second time.
   </P
></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"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.variables.variable.html"
ACCESSKEY="N"
>Next</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"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Variable variables</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>