php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #18480 Undocumented global use
Submitted: 2002-07-22 18:47 UTC Modified: 2002-07-23 10:44 UTC
From: seb at delahaye dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.2.1 OS: all
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: seb at delahaye dot net
New email:
PHP Version: OS:

 

 [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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-22 18:54 UTC] rasmus@php.net
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 19:00 UTC] seb at delahaye dot net
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 19:05 UTC] rasmus@php.net
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-23 10:27 UTC] seb at delahaye dot net
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 &quot;3&quot;.  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-23 10:40 UTC] cynic@php.net
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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 11 13:01:28 2024 UTC