ID: 18480 Updated by: [EMAIL PROTECTED] Reported By: [EMAIL PROTECTED] Status: Open Bug Type: Documentation problem Operating System: all PHP Version: 4.2.1 New Comment:
I really don't understand why this point would have confused you. The only variables you can access inside a function are the variables local to that specific function. If you want to access a variable defined outside of the function (ie. a global) you have to specify you want to access that global variable. There is no way for a function to access another function's locally scoped variables. Previous Comments: ------------------------------------------------------------------------ [2002-07-22 19:00:11] [EMAIL PROTECTED] For me, the fact that it was needed to create a global variable in one function to access to in another was not clear. Specially because it's not the case for vars that are outside any function. I don't think it's something evident. My patch probably doesn't explain it well, I'll try to make one more clear tomorrow (yup, it's damn too late here). ------------------------------------------------------------------------ [2002-07-22 18:54:14] [EMAIL PROTECTED] 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. ------------------------------------------------------------------------ [2002-07-22 18:47:58] [EMAIL PROTECTED] 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 -- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php