Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-release > by-pkgid > ebb1914cf182a88528b4547490db1dd8 > files > 1612

kdewebdev-quanta-doc-3.5.9-2mdv2008.1.x86_64.rpm

<HTML
><HEAD
><TITLE
>Variable scope</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="manual.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"></HEAD
><BODY
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><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"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 7. Variables</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.variables.variable.html"
>Next</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
>&#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"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$a = 1;
  3&nbsp;include "b.inc";
  4&nbsp;    </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    Here the $a variable will be available within the included b.inc
    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
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp; 
  2&nbsp;$a = 1; /* global scope */ 
  3&nbsp;
  4&nbsp;Function Test () { 
  5&nbsp;    echo $a; /* reference to local scope variable */ 
  6&nbsp;} 
  7&nbsp;
  8&nbsp;Test ();
  9&nbsp;    </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 $a 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"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$a = 1;
  3&nbsp;$b = 2;
  4&nbsp;
  5&nbsp;Function Sum () {
  6&nbsp;    global $a, $b;
  7&nbsp;
  8&nbsp;    $b = $a + $b;
  9&nbsp;} 
 10&nbsp;
 11&nbsp;Sum ();
 12&nbsp;echo $b;
 13&nbsp;    </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    The above script will output "3".  By declaring $a and
    $b 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 $GLOBALS array.  The previous example can
    be rewritten as:
   </P
><DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;$a = 1;
  3&nbsp;$b = 2;
  4&nbsp;
  5&nbsp;Function Sum () {
  6&nbsp;    $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
  7&nbsp;} 
  8&nbsp;
  9&nbsp;Sum ();
 10&nbsp;echo $b;
 11&nbsp;    </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    The $GLOBALS 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.
   </P
><P
>&#13;    Another important feature of variable scoping is the
    <I
CLASS="emphasis"
>static</I
> 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"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;Function Test () {
  3&nbsp;    $a = 0;
  4&nbsp;    echo $a;
  5&nbsp;    $a++;
  6&nbsp;}
  7&nbsp;    </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    This function is quite useless since every time it is called it
    sets $a to 0 and prints "0".  The $a++ which increments
    the variable serves no purpose since as soon as the function exits
    the $a variable disappears.  To make a useful counting function
    which will not lose track of the current count, the $a variable is
    declared static:</P
><DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;Function Test () {
  3&nbsp;    static $a = 0;
  4&nbsp;    echo $a;
  5&nbsp;    $a++;
  6&nbsp;}
  7&nbsp;    </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    Now, every time the Test() function is called it will print the
    value of $a 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 $count to know when to stop:
   </P
><DIV
CLASS="informalexample"
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
>  1&nbsp;
  2&nbsp;Function Test () {
  3&nbsp;    static $count = 0;
  4&nbsp;
  5&nbsp;    $count++;
  6&nbsp;    echo $count;
  7&nbsp;    if ($count &#60; 10) {
  8&nbsp;        Test ();
  9&nbsp;    }
 10&nbsp;    $count--;
 11&nbsp;}
 12&nbsp;    </PRE
></TD
></TR
></TABLE
><P
></P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="language.variables.predefined.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="manual.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.variables.variable.html"
>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"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Variable variables</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>