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!
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:
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.
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.
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.
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.
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.
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.
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.
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).
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:
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.
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: