Global vars

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
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Global vars

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

globals are evil.. why do you insist on using them?

Sessions seem much more appropriate than use of globals here..
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

A Session is a global though.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

agtlewis wrote:A Session is a global though.
Sessions ($_SESSION) is a *super-global*, which is extremely different from a global.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :P

Cheers :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

This rant sums it up better than I can

http://www.sitepoint.com/forums/showpos ... ostcount=7
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Roja wrote: To help clarify a bit, enjoy the fine manual:
http://us2.php.net/manual/en/language.v ... perglobals
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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Jcart wrote:This rant sums it up better than I can

http://www.sitepoint.com/forums/showpos ... ostcount=7
:lol:

Can't say I agree with everything that guys says. Some valid points, but some naive points as well.

Ah well :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

enough squabbling.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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).
Post Reply