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

Post by JellyFish »

Thank you all. Wow I didn't realize how much attention my post got and I thank you all for that.

The fact of the matter is I'm vary new at php and this information is really, really useful. I'm assuming that what you mean when you say globals are evil or bad, is that, if I try and use a variable name in another script, not knowing that I used this name as a global before, that the value of that name is still specified my a previuos script that uses the name as a global.

Thanks agian for all these post. Just checking if I got this right, when I want to asign a value to my global variable I:

Code: Select all

$GLOBAL['error_msg'] = "Username or Password is Incorrect.";
And when I want to display it I ether:

Code: Select all

echo "$error_msg";
Or:

Code: Select all

echo "$GLOBAL['error_msg'];
Most likely I'd use the $GLOBAL to echo the contents instade of $error_msg but I'm not quite sure.

Thanks.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

echo $GLOBAL['error_msg'];
not

Code: Select all

echo "$GLOBAL['error_msg'];


Using quotes where they are not needed is really messing you up


----

@ Roja, I was wrong.. thanks for putting me in my place :wink:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Well I figured I would ruffle some feathers with that comment about a Session being a global. I understand what Roja is saying about how Superglobals have different properties than regular globals in that you cannot unset them etc. All I am saying is that even though it may have different properties, it's still a global variable, perhaps even more so then a regular global. Sorry if I made you upset Roja.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Thank you jshpro2. I don't usually put quotes when echoing a var or something like that. Thanks for answering my question and thanks for correcting me on that. :)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Well apparently I'm doing something wrong. I am setting the $GLOBAL['error_msg'] like so.

Code: Select all

$GLOBAL['error_msg'] = "Incorrect Username or Password.";
header("Location: show_login.php");
exit;
And I am trying to display like this.

Code: Select all

echo $GLOBAL['error_msg'];
And Where I'm trying to display it and where I'm trying to set the var are in to seperate places and I don't think that that is a problem considering the fact that that is what globals are for.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

EasyGoing wrote:Well apparently I'm doing something wrong. I am setting the $GLOBAL['error_msg'] like so.
Global variables won't be transferred to another script. You may want to put it in a Session.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

once again, why are you using globals?? If need be stick it in a session.

psuedo:

Code: Select all

session_start();

... page stuff ?


if (IS_AN_ERROR) {
   $_SESSION['error_message'] = "Incorrect Username or Password.";
}
Debugging globals is just too much of a mess, hear my warning.

p.s. its $GLOBAL`S`

agtlewis wrote:
EasyGoing wrote:Well apparently I'm doing something wrong. I am setting the $GLOBAL['error_msg'] like so.
Global variables won't be transferred to another script. You may want to put it in a Session.
What do you mean? Of course they will as long as the script has been included at some point or another during execution.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Global vars

Post by Christopher »

EasyGoing wrote: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.
OK, I believe that you now know that you use the syntax "global $myvar;" inside a function to make a variable in the global namespace visible within the function.

But hopefully even you can see that your comment "For some reason it doesn't work cause I assume that the variable isn't global" is pretty troublesome because you are not clear where the variables you need are located. And from this thread you can probably tell that there is something up with using globals. And yours is a good example of the problems of globals. Because they are not limited in scope and can be potentially be used in many unrelated pieces of code -- they can make it difficult to know where the cause of the problem is.

Post some code and perhaps we can show you how to change to not need globals.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Jcart wrote:
agtlewis wrote:
EasyGoing wrote:Well apparently I'm doing something wrong. I am setting the $GLOBAL['error_msg'] like so.
Global variables won't be transferred to another script. You may want to put it in a Session.
What do you mean? Of course they will as long as the script has been included at some point or another during execution.
What I mean is that if you set a global variable to a value in 1 page such as $GLOBALS['ERROR'] = "Blah"; and then do a header redirect to another page, that variable no longer exists, which I believe is why this dudes script doesn't work and it's also why he should use a session.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Yes agtlewis Your right on. The whole time I thought $GLOBALS would make you able to take a var and have itavialable to all scripts. Is there another way to make a var avialable without making it a session? This would help me very much, but if not I'll just have to make an exeption. Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the problem with using sessions?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

I guess your right there is really nothing wrong with using sessions. But if there is another way I think it'll be good to expand my knowledge of this situation. If there is then I'd try it, but if not I'd just use sessions.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

EasyGoing wrote:Is there another way to make a var avialable without making it a session? This would help me very much, but if not I'll just have to make an exeption.
There are three basic ways to persist data in PHP:

1. Pass the values between scripts via HTTP

2. Put the values into a Cookie and have the client persist them

3. Have the server persist the values via the Session system (or in conjunction with it and a file, database or shared memory scheme).
(#10850)
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Post by JellyFish »

Well how can I just make a variable global to where all scripts on my page can access the value without having to use a session? I think there's some way to do this like:

Code: Select all

global $my_var = "blablabla";
And I think this is one way to make it global and if not then you can ether correct me on this or insist that I use sessions.

Thank you guy's for all of your time and help I really appreciate all these post, I'm rapping this up with this post and all later post are appreciated. Thanks all and good night, well at least where I am. :lol:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

What your trying to do is not possible because all of the variables are released from memory once your script is finished executing. The only other way you can do it without using a session or cookie is to redirect to the page with a url like this:

Code: Select all

header("Location: http://yourdomain.com/thispage.php?message=" . urlencode($message));
Then in the receiving page you can retrieve the message:

Code: Select all

$message = strip_tags($_GET['message']);
I haven't tested that and I would never use it myself but that is a way it can be done.
Post Reply