[PHP-DOC] Bug #18480 Updated: Undocumented global use
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: gosh, and how would the KeepVariables() function know to access the *global* variables $a and $b if you didn't tell it to? this is IMO just a matter of common sense, so please, stop this thread. Previous Comments: [2002-07-23 10:27:56] [EMAIL PROTECTED] Yes. I knew I had to declare the var as global is the function where I wanted to use it. What was not clear for me was that I needed to declare it as global in the two functions. If I take the functions from the patch as example, declaring $a and $b as global in Sum() was evident, but declaring them as global also in KeepVariables() was not evident. Of course, when you *know* it, it's clear, but when you don't know it, and since the doc isn't IMHO clear about this, it's not evident at all. This patch should be more clear (hopefully): Index: en/language/variables.xml === RCS file: /repository/phpdoc/en/language/variables.xml,v retrieving revision 1.52 diff -u -r1.52 variables.xml --- en/language/variables.xml 3 Jul 2002 22:51:23 - 1.52 +++ en/language/variables.xml 23 Jul 2002 14:26:36 - @@ -375,8 +375,10 @@ The above script will output quot;3quot;. By declaring varname$a/varname and varname$b/varname 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. +global version. In the same way, if you want to access in a function +to a var from another function, declare it as global in both functions. +There is no limit to the number of global variables that can be +manipulated by a function. /simpara simpara [2002-07-22 19:05:35] [EMAIL PROTECTED] 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. [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 - 1.52 +++ en/language/variables.xml 22 Jul 2002 22:34:16 - @@ -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
[PHP-DOC] Bug #18480 Updated: Undocumented global use
ID: 18480 Updated by: [EMAIL PROTECTED] Reported By: [EMAIL PROTECTED] -Status: Open +Status: Closed Bug Type: Documentation problem Operating System: all PHP Version: 4.2.1 Previous Comments: [2002-07-23 10:40:46] [EMAIL PROTECTED] gosh, and how would the KeepVariables() function know to access the *global* variables $a and $b if you didn't tell it to? this is IMO just a matter of common sense, so please, stop this thread. [2002-07-23 10:27:56] [EMAIL PROTECTED] Yes. I knew I had to declare the var as global is the function where I wanted to use it. What was not clear for me was that I needed to declare it as global in the two functions. If I take the functions from the patch as example, declaring $a and $b as global in Sum() was evident, but declaring them as global also in KeepVariables() was not evident. Of course, when you *know* it, it's clear, but when you don't know it, and since the doc isn't IMHO clear about this, it's not evident at all. This patch should be more clear (hopefully): Index: en/language/variables.xml === RCS file: /repository/phpdoc/en/language/variables.xml,v retrieving revision 1.52 diff -u -r1.52 variables.xml --- en/language/variables.xml 3 Jul 2002 22:51:23 - 1.52 +++ en/language/variables.xml 23 Jul 2002 14:26:36 - @@ -375,8 +375,10 @@ The above script will output quot;3quot;. By declaring varname$a/varname and varname$b/varname 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. +global version. In the same way, if you want to access in a function +to a var from another function, declare it as global in both functions. +There is no limit to the number of global variables that can be +manipulated by a function. /simpara simpara [2002-07-22 19:05:35] [EMAIL PROTECTED] 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. [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. The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/18480 -- Edit this bug report at http://bugs.php.net/?id=18480edit=1 -- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DOC] Bug #18480 Updated: Undocumented global use
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: 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] [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 - 1.52 +++ en/language/variables.xml 22 Jul 2002 22:34:16 - @@ -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=18480edit=1 -- PHP Documentation Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php