TRUE == FALSE ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

Post by bindermichi »

feyd wrote:"global $foo" is identical to $GLOBALS['foo'], they're interchangable.

Code: Select all

[feyd@home]>php -r "$foo = true; function foo(){global $foo; $foo = 1234; $GLOBALS['foo'] = 'asdf'; var_dump($foo);} foo(); var_dump($foo);"
string(4) "asdf"
string(4) "asdf"
Ok, didn't read that way :?

...but to prevent your example I used the static, which didn't work out right...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"static" doesn't do anything in the global scope; it's global, there's nowhere for it to go.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

bindermichi wrote:Hmm... as far as i read the manual, there's a difference between "$GLOBALS[''];" and "global $var;"
By declaring $a and $b global within the function, all references to either variable will refer to the global version.
Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.
...and if I learned one thing in programming class... it's never to use superglobals, unless you have to :wink:
I believe you are confusing two ways of accessing a global variable (though the global keyword and through the $GLOBALS superglobal) with the concept that globals are bad. This is the case (that you should try to avoid the use of global variables) but you ARE still using them with either method of access. Changing how you access a global varaible does not reduce your dependancy on it.

In my own code I entirely avoid the global keyword. I would much rather grab a global from the big, ugly $GLOBALS['some_var'] as a constant reminder to me that I should be execising caution when doing so.
User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

Post by bindermichi »

sweatje wrote: I believe you are confusing two ways of accessing a global variable (though the global keyword and through the $GLOBALS superglobal) with the concept that globals are bad. This is the case (that you should try to avoid the use of global variables) but you ARE still using them with either method of access. Changing how you access a global varaible does not reduce your dependancy on it.

In my own code I entirely avoid the global keyword. I would much rather grab a global from the big, ugly $GLOBALS['some_var'] as a constant reminder to me that I should be execising caution when doing so.
K, ... guess it's monday after all :roll:

Well, it works with the global array, so I guess for debugging the functions this will do.
Post Reply