note 25332 deleted from language.variables.scope by nlopess
| From: | nlopess@php.net | Date: | Sat, 27 Dec 2003 15:11:29 +0000 |
| Subject: | note 25332 deleted from language.variables.scope by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-62561@lists.php.net to get a copy of this message | ||
Note Submitter: morthanpurpl@yahoo.com
----
I have spent most of a day trying to figure out this business of globally scoped variables and
functions. At long last I have figured out what is not made clear in the hundreds of pieces of
documentation, notes, mail & forum messages, etc. What it amounts to is this:
You MUST use a variable in a global context FIRST, even if all you do is initialize it, as in:
$a = '';
Then:
function dumdum()
{
global $a;
$a = "iam";
}
... will place the value "iam" in the GLOBAL variable $a.
It appears (and this is day 2 of PHP for me, so I could be grossly wrong) that without the $a =
'' before the function call, the operation of global within a function is actually
implemented as:
"Use a global variable if there is one, otherwise ignore the global declaration and create/use
a function-local variable."
This also appears true even if the infamous "register_globals" is "On".
The reason this may be confusing is the oft-repeated "PHP does not require you to declare
variables before using them."
Except, it seems, in this case.