Page 1 of 3
Global vars
Posted: Tue Feb 28, 2006 4:31 pm
by JellyFish
How do I make a variable global? I'm trying to make this script or login script that displays a forum that allows you to enter a username and password and then it goes to do_login.php and takes and checks if the username or password is correct and if not it sets a variable then headers back to the login script. For some reason it doesn't work cause I assume that the variable isn't global. All I need to know is how to make a variable global.
Thanks.
Posted: Tue Feb 28, 2006 4:38 pm
by neophyte
Inside a function:
Code: Select all
$GLOBALS['foobar'] = 'thisthing';
$foo = 'something';
function some {
global $foo;
echo $GLOBALS['foobar'];
}
But you may just want to pass by reference too.
Posted: Tue Feb 28, 2006 5:01 pm
by John Cartwright
globals are evil.. why do you insist on using them?
Sessions seem much more appropriate than use of globals here..
Posted: Tue Feb 28, 2006 6:33 pm
by Benjamin
A Session is a global though.
Posted: Tue Feb 28, 2006 6:40 pm
by Roja
agtlewis wrote:A Session is a global though.
Sessions ($_SESSION) is a *super-global*, which is extremely different from a global.
Posted: Tue Feb 28, 2006 6:42 pm
by Benjamin
Roja wrote:agtlewis wrote:A Session is a global though.
Sessions ($_SESSION) is a *super-global*, which is extremely different from a global.
Ok, I didn't realize that, I just categorized it as a Global.
Posted: Tue Feb 28, 2006 6:55 pm
by Roja
agtlewis wrote:Roja wrote:agtlewis wrote:A Session is a global though.
Sessions ($_SESSION) is a *super-global*, which is extremely different from a global.
Ok, I didn't realize that, I just categorized it as a Global.
To help clarify a bit, enjoy the fine manual:
http://us2.php.net/manual/en/language.v ... perglobals
Posted: Tue Feb 28, 2006 6:59 pm
by alex.barylski
Jcart wrote:globals are evil.. why do you insist on using them?
Sessions seem much more appropriate than use of globals here..
I can't say GLOBALS are the best...but I see no evil in them...
GLOBALS become a potential problem when they get re-assigned somewhere in code and you can't figure out where - I think we all would agree on that.
Perhaps a silly approach, but I have always enjoyed using PHP's super globals and using some crazy namespace to avoid easily over writting variables unintentionally...
Code: Select all
$GLOBALS['CODENAME']['SECTION']['SETTINGS']['NAME'] = 'Some value';
Yes this is a pain to repeat, but at least when you do you know you mean to.
They have the advantage over constants/defines, that they can be assigned something other than scalars and their values can be changed.
I'll be honest I have very little working experience with SESSIONS but i understand the concept.
I've always sided with Super globals when I could, because for one, their hassle free to setup/init and two, I'm lazy...
I prefer to use GLOBALS for certain config settings...over a database, or INI or XML file. Their speed, ease of use and flexibility I've always appreciated.
However in this case, it does seem like a SESSION or COOKIE (which I would use) would probably do better off...
I just wanted to make my stance for the use of GLOBALS
Cheers

Posted: Tue Feb 28, 2006 7:11 pm
by John Cartwright
Posted: Tue Feb 28, 2006 7:36 pm
by Benjamin
Thanks Roja but I actually have a copy of the manual on by box. A Session is a global variable that can be set or retrieved anywhere in the script, just like a global variable. If you want to call it a Superglobal and tell me that makes all the difference that is fine but it's still a global variable.
Posted: Tue Feb 28, 2006 7:39 pm
by alex.barylski
Can't say I agree with everything that guys says. Some valid points, but some naive points as well.
Ah well

Posted: Tue Feb 28, 2006 7:39 pm
by feyd
enough squabbling.
Posted: Tue Feb 28, 2006 7:43 pm
by Roja
agtlewis wrote:A Session is a global variable that can be set or retrieved anywhere in the script, just like a global variable. If you want to call it a Superglobal and tell me that makes all the difference that is fine but it's still a global variable.
Ahem.
No, there is a difference, and you should read the page I linked to understand the difference. $_SESSION is a
superglobal, not a global.
It will always exist - unlike a global. It will always be available - unlike a global. It will be available even inside of a function or class without requesting it - unlike a global.
I am not being pedantic about naming. There are significant differences. Stop calling a flounder a
whale, and insulting me when I offer a helpful correction that the flounder isn't a whale.
Posted: Tue Feb 28, 2006 7:44 pm
by josh
Hockey wrote:
Code: Select all
$GLOBALS['CODENAME']['SECTION']['SETTINGS']['NAME'] = 'Some value';
Yes this is a pain to repeat, but at least when you do you know you mean to.
Or you could do something that made more sense and use classes
$database -> connection -> password, if you find yourself *needing* globals its just bad design, although they can be usefull in some situations there is always another, better way to do it.
agtlewis wrote:If you want to call it a Superglobal and tell me that makes all the difference that is fine but it's still a global variable.
Superglobals are globals, and
more.. I think the more part is where the confusion came from
Posted: Tue Feb 28, 2006 7:47 pm
by Roja
jshpro2 wrote:Superglobals are globals, and more.. I think the more part is where the confusion came from
Actually, they aren't. You can unset a global, and it no longer exists. Thats not true for a superglobal. Thus, a superglobal is NOT a global (even with ... and more).