note 25332 added to language.variables.scope

From: Date: Thu, 19 Sep 2002 23:54:16 +0000
Subject: note 25332 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-37002@lists.php.net to get a copy of this message
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. -- http://www.php.net/manual/en/language.variables.scope.php http://master.php.net/manage/user-notes.php?action=edit+25332 http://master.php.net/manage/user-notes.php?action=delete+25332 http://master.php.net/manage/user-notes.php?action=reject+25332

« previous php.notes (#37002) next »