Bug #18480 Updated: Undocumented global use

From: Date: Mon, 22 Jul 2002 22:54:15 +0000
Subject: Bug #18480 Updated: Undocumented global use
References: 1  Groups: php.bugs php.doc 
Request: Send a blank email to php-bugs+get-14810@lists.php.net to get a copy of this message
ID: 18480 Updated by: rasmus@php.net Reported By: seb@delahaye.net Status: Open Bug Type: Documentation problem Operating System: all PHP Version: 4.2.1 New Comment: That's not a different use though. You are simply creating a global variable in one function and accessing it in another. I don't think this patch clarifies anything. Perhaps the fact that you can create global variables inside a function is not documented well enough, but a much simpler documentation fix can take care of that. Previous Comments: ------------------------------------------------------------------------ [2002-07-22 18:47:58] seb@delahaye.net One of the uses of "global $var;" seems not to be documented. Here's a tiny patch to fix that. --beginning-- Index: en/language/variables.xml =================================================================== RCS file: /repository/phpdoc/en/language/variables.xml,v retrieving revision 1.52 diff -u -u -r1.52 variables.xml --- en/language/variables.xml 3 Jul 2002 22:51:23 -0000 1.52 +++ en/language/variables.xml 22 Jul 2002 22:34:16 -0000 @@ -380,6 +380,47 @@ </simpara> <simpara> + Please note that if you want to access in your function to a variable + which is in another function, all that through a global variable, + you'll need to declare it as a variable in the two functions. + An example, always with our function Sum(): + </simpara> + + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php + +function KeepVariables() +{ + global $a, $b; + + $a = 1; + $b = 2; +} + +function Sum() +{ + global $a, $b; + + $b = $a + $b; +} + +KeepVariables(); +Sum(); +echo $b; +?> +]]> + </programlisting> + </informalexample> + + <simpara> + If we hadn't declared <varname>$a</varname> and <varname>$b</vargame> + as globals in the KeepVariables() function, the script would have + displayed nothing. + </simpara> + + <simpara> A second way to access variables from the global scope is to use the special PHP-defined <varname>$GLOBALS</varname> array. The previous example can be rewritten as: --end-- If it can be useful, I can also write a french version of it, since I'm french. HTH. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=18480&edit=1

« previous php.bugs (#14810) next »