|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-07-22 18:47 UTC] seb at delahaye dot 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 18:00:01 2025 UTC |
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 -0000 1.52 +++ en/language/variables.xml 23 Jul 2002 14:26:36 -0000 @@ -375,8 +375,10 @@ The above script will output "3". 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>