philip Fri Jul 18 01:43:49 2003 EDT Modified files: /phpdoc/en/language variables.xml Log: Created sections for static and global as requested in bug #16234. Also made informal examples formal. Index: phpdoc/en/language/variables.xml diff -u phpdoc/en/language/variables.xml:1.67 phpdoc/en/language/variables.xml:1.68 --- phpdoc/en/language/variables.xml:1.67 Sun Jul 6 12:40:27 2003 +++ phpdoc/en/language/variables.xml Fri Jul 18 01:43:49 2003 @@ -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 @@ 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 @@ echo $b; ?> ]]> - </programlisting> - </informalexample> + </programlisting> + </example> + </para> <simpara> The above script will output "3". By declaring @@ -399,9 +407,10 @@ 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 @@ echo $b; ?> ]]> - </programlisting> - </informalexample> + </programlisting> + </example> + </para> <simpara> The <varname>$GLOBALS</varname> array is an associative array with @@ -429,9 +439,9 @@ 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 @@ ?> ]]> </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 @@ 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 @@ } ?> ]]> - </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 @@ 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 @@ } ?> ]]> - </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 @@ 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 @@ } ?> ]]> - </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 @@ variable, it's not <emphasis>remembered</emphasis> when you call the <literal>&get_instance_ref()</literal> function a second time. </simpara> - - + </sect2> </sect1> <sect1 id="language.variables.variable">
-- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php