Created sections for static and global as requested in bug #16234. Also

made informal examples formal.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@135349 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Philip Olson 2003-07-18 05:43:49 +00:00
parent 14787bd7d7
commit 7ba4269019

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.67 $ -->
<!-- $Revision: 1.68 $ -->
<chapter id="language.variables">
<title>Variables</title>
@ -362,11 +362,18 @@ Test();
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:
that function.
</simpara>
<informalexample>
<programlisting role="php">
<sect2 id="language.variables.scope.global">
<title>The global keyword</title>
<simpara>
First, an example use of <literal>global</literal>:
</simpara>
<para>
<example>
<title>Using global</title>
<programlisting role="php">
<![CDATA[
<?php
$a = 1;
@ -383,8 +390,9 @@ Sum();
echo $b;
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</example>
</para>
<simpara>
The above script will output &quot;3&quot;. By declaring
@ -399,9 +407,10 @@ echo $b;
the special PHP-defined <varname>$GLOBALS</varname> array. The
previous example can be rewritten as:
</simpara>
<informalexample>
<programlisting role="php">
<para>
<example>
<title>Using <varname>$GLOBALS</varname> instead of global</title>
<programlisting role="php">
<![CDATA[
<?php
$a = 1;
@ -416,8 +425,9 @@ Sum();
echo $b;
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</example>
</para>
<simpara>
The <varname>$GLOBALS</varname> array is an associative array with
@ -429,9 +439,9 @@ echo $b;
Here's an example demonstrating the power of superglobals:
</simpara>
<para>
<informalexample>
<programlisting role="php">
<example>
<title>Example demonstrating superglobals and scope</title>
<programlisting role="php">
<![CDATA[
<?php
function test_global()
@ -450,9 +460,12 @@ function test_global()
?>
]]>
</programlisting>
</informalexample>
</example>
</para>
</sect2>
<sect2 id="language.variables.scope.static">
<title>Using static variables</title>
<simpara>
Another important feature of variable scoping is the
<emphasis>static</emphasis> variable. A static variable exists
@ -460,9 +473,10 @@ function test_global()
when program execution leaves this scope. Consider the following
example:
</simpara>
<informalexample>
<programlisting role="php">
<para>
<example>
<title>Example demonstrating need for static variables</title>
<programlisting role="php">
<![CDATA[
<?php
function Test ()
@ -473,9 +487,9 @@ function Test ()
}
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</example>
</para>
<simpara>
This function is quite useless since every time it is called it
sets <varname>$a</varname> to <literal>0</literal> and prints
@ -485,9 +499,10 @@ function Test ()
counting function which will not lose track of the current count,
the <varname>$a</varname> variable is declared static:
</simpara>
<informalexample>
<programlisting role="php">
<para>
<example>
<title>Example use of static variables</title>
<programlisting role="php">
<![CDATA[
<?php
function Test()
@ -498,9 +513,9 @@ function Test()
}
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</example>
</para>
<simpara>
Now, every time the Test() function is called it will print the
value of <varname>$a</varname> and increment it.
@ -515,9 +530,10 @@ function Test()
simple function recursively counts to 10, using the static
variable <varname>$count</varname> to know when to stop:
</simpara>
<informalexample>
<programlisting role="php">
<para>
<example>
<title>Static variables with recursive functions</title>
<programlisting role="php">
<![CDATA[
<?php
function Test()
@ -533,13 +549,19 @@ function Test()
}
?>
]]>
</programlisting>
</informalexample>
</programlisting>
</example>
</para>
</sect2>
<sect2 id="language.variables.scope.references">
<title>References with global and static variables</title>
<simpara>
The Zend Engine 1, driving <literal>PHP4</literal>, implements the
<literal>static</literal> and <literal>global</literal> modifier for
variables in terms of references. For example, a true global variable
<link linkend="language.variables.scope.static">static</link> and
<link linkend="language.variables.scope.global">global</link> modifier
for variables in terms of <link linkend="language.references">
references</link>. For example, a true global variable
imported inside a function scope with the <literal>global</literal>
statement actually creates a reference to the global variable. This can
lead to unexpected behaviour which the following example addresses:
@ -643,8 +665,7 @@ Static object: object(stdClass)(1) {
variable, it's not <emphasis>remembered</emphasis> when you call the
<literal>&amp;get_instance_ref()</literal> function a second time.
</simpara>
</sect2>
</sect1>
<sect1 id="language.variables.variable">