RE: [PHP] PHP globals aren't really globals
Addressed to: Maxim Maletsky <[EMAIL PROTECTED]> [EMAIL PROTECTED] Christian Dechery [mailto:[EMAIL PROTECTED]] ** Reply to note from Maxim Maletsky <[EMAIL PROTECTED]> Fri, 16 Feb 2001 12:18:27 +0900 > > I never used 50 of them, but I think there's such a thing as 'register > globals' wchich makes all if the globals become available inside your > funcs.. no idea how to use it ... I feel like I've seen it in = > someone > else's code ... > > correct me if I am wrong ... Consider your self corrected... :) register_globals is about placing GET/PUT/COOKIE/ENVIRONMENT variables into the global scope. It has nothing to do with functions. Rasmus explained why you need to declare variables as global in EVERY function that uses them here: http://marc.theaimsgroup.com/?l=php-general&m=97984136422910&w=2 and here: http://marc.theaimsgroup.com/?l=php-general&m=97717866712033&w=2 You may as well get used to it, it isn't going to change any time soon... Rick Widmer Internet Marketing Specialists http://www.developersdesk.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] PHP globals aren't really globals
I never used 50 of them, but I think there's such a thing as 'register globals' wchich makes all if the globals become available inside your funcs.. no idea how to use it ... I feel like I've seen it in someone else's code ... correct me if I am wrong ... Cheers, Maxim Maletsky -Original Message- From: Christian Dechery [mailto:[EMAIL PROTECTED]] Sent: Wednesday, February 14, 2001 11:25 PM To: [EMAIL PROTECTED] Subject: [PHP] PHP globals aren't really globals I've been programming in C all my life, and now I just started developing in PHP and I'm really enjoying it, it has all the missing improvements that C needed to be more user-likely. But one thin I can't get, how can PHP call a variabel global, if it isn't global. A global var, is a var defined outside all functions and it's available to all and altered by all, without having to redefine or call the var again. In PHP, for a var to be global you have to add a 'global $var' inside the function u want to use it. THis is not nice, what about if u have a form with 50 fields and want a function to validate all of them, u have to pass them all to the function or build a little piece of code to make all th $GLOBALS local right? Is this really the idea of global vars? . [ Christian Dechery ] . Webdeveloper @ Tá Na Mesa! . Listmaster @ Gaita-L . http://www.tanamesa.com.br -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] PHP globals aren't really globals
It's somewhat annoying to have to tell the function which variables are global sometimes but overall it makes it easier to avoid stupid mistakes. It is a good push to make me less prone to making things global. Usually I just avoid using globals and then pack what I do use into appropiate arrays and thus save myself most the hassle. In several thousand lines of code I may have one or two global vars called. Boget, Chris wrote: >>> Yes. But with regards to form variables, all you need to do is >>> make one variable global: >>> $HTTP_POST_VARS >>> it is an associative array that contains all the post variables from >>> the form. Make it global and just loop through it. >> >> exactly, u need to loop to an array to get the globals u want. > > > No, to the the variables/values that you want. > >> this is not the idea of global, global is global... if it's set outside >> of ALL functions it should be available 'with no extra code' >> to ALL functions, it works that way in all programming language >> I'm familiar with... why not in PHP? that's what I wanna know, >> why globals in PHP doesn't really work like globals? > > > Perhaps I'm missing something. In order to access the value of any > variable defined outside the scope of the function, you have to declare > it as "global". This is true in every single instance. If you do not, then > you cannot access the value of that variable. While it's been a while > since I've worked with C, I seem to recall it being the case there as > well. And in Pascal. And in VB. Again, it's been a while so I could be > wrong (but don't think so). > Also, PHP was written in C. Why would they institute a behaviour > that was radically different than what is part of the parent language? > > Chris > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] PHP globals aren't really globals
> I believe you use globals $var to ACCESS a global variable, not to > define. That's correct. To be perfectly correct here, what you are doing is creating a reference to the global variable. Think of it as an alias to the same variable as the global variable. global $foo; would be equivalent to: $foo = & $GLOBALS['foo']; With the latter giving you the ability to use a different name for the local reference to the globally-scoped variable. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] PHP globals aren't really globals
I believe you use globals $var to ACCESS a global variable, not to define. If I understand correctly, and please correct me if I am wrong, this a way to ge around the name space... ex. for something like $my_var = 10; function test () { $my_var = 15; } because of these variables are in a different name space, they are not the same. If you intended to change the first variable, then you should do something like function test() { globals $my_var; $my_var = 15; } This would change the value of $my_var from 10 to 15. Please correct me, if I'm wrong. __John Monfort_ _+---+_ P E P I E D E S I G N S www.pepiedesigns.com "The world is waiting, are you ready?" -+___+- On Wed, 14 Feb 2001, Christian Dechery wrote: > I've been programming in C all my life, and now I just started developing > in PHP and I'm really enjoying it, it has all the missing improvements that > C needed to be more user-likely. > > But one thin I can't get, how can PHP call a variabel global, if it isn't > global. > A global var, is a var defined outside all functions and it's available > to all and altered by all, without having to redefine or call the var again. > > In PHP, for a var to be global you have to add a 'global $var' inside the > function u want to use it. THis is not nice, what about if u have a form > with 50 fields and want a function to validate all of them, u have to pass > them all to the function or build a little piece of code to make all th > $GLOBALS local right? > > Is this really the idea of global vars? > > > . [ Christian Dechery ] > . Webdeveloper @ Tá Na Mesa! > . Listmaster @ Gaita-L > . http://www.tanamesa.com.br > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] PHP globals aren't really globals
>The idea is to avoid really nasty scope-related bugs that are common in >C >code that uses lots of global variables. Years and years ago when I wrote >the first version of PHP I was working for a telco writing software for >a >large telephone switch. The code was huge and extremely ugly. Global >variables everywhere. The team had been chasing a bug for about a week >when I got stubborn and decided it was time to kill the bug. I printed >out all the source code and laid it out in a long hallway as I crawled >along with different coloured pens and manually traced my way through it >as none of the debuggers we had at the time were of any use. After >countless hours the bug turned out to be inside a function that silently >modified a global variable which affected another piece of code in a >completely different part of the program. > >I swore I would not have the same problem in PHP and thus the requirement >for people to be explicit about using global variables inside functions. >Hopefully it also forces a little bit of structure and organization on >people. thanks... now I have an answer I can swallow... cuz I simply didn't get why PHP (which came from C) had a differente behaviour according to globals. I never had any problem handling them, but in the other hand I never had 50 of them in a program. I can understand that making global every single form variable would take a whole lot of control by the compiler... I just came with a nice foreach() to make my globals local, and I have to put them in ALL my functions that handles form vars... I'll consider using arrays in my next forms but hey, don't worry... PHP really kicks ASP ass in all matters! :) cya . [ Christian Dechery ] . Webdeveloper @ Tá Na Mesa! . Listmaster @ Gaita-L . http://www.tanamesa.com.br -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] PHP globals aren't really globals
> Rasmus wrote: > >I swore I would not have the same problem in PHP and thus the requirement > >for people to be explicit about using global variables inside functions. > >Hopefully it also forces a little bit of structure and organization on > >people. > > Hear, hear and thank you Rasmus. It seems to me that the several other ways > folks have listed to solve this one example indicates that we don't really > need the temporary convenience of wide open globals (temporary as in it > doesn't end up very convenient when they create problems). I was always tought very simply, don't use globals :) However, that isn't always an option. I have no problems with the way PHP does things. I think its a good idea for the shelter. What I find interesting is that it seems when you are first learning a language, you use more globals, than when you are more experienced with it. When I first started programmig in C, 99% of my variables were global. Look at any of my C/C++ code nowadays, over 10 years later, and you will be hard pressed to find a global. PHP is still new to me, so i'm still trying to use alot of globals. Because of the way PHP handles it, i'm jumping through a few hoops. That is annoying, but I don't consider it a problem, because as I get better, I know I will go back through and rewrite things to not use globals. I guess my confusion is this: I am passing all my variables around between scripts by sending them through the URLs (index.php?mode=index). I guess what "annoys" me, is I would expect those to be available from inside the functions, but they aren't. - John Vanderbeck - Admin, GameDesign > > Visit the Gates Motel webgame: > http://www.gameslate.com/gatesmotel/ > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] PHP globals aren't really globals
Rasmus wrote: >I swore I would not have the same problem in PHP and thus the requirement >for people to be explicit about using global variables inside functions. >Hopefully it also forces a little bit of structure and organization on >people. Hear, hear and thank you Rasmus. It seems to me that the several other ways folks have listed to solve this one example indicates that we don't really need the temporary convenience of wide open globals (temporary as in it doesn't end up very convenient when they create problems). Visit the Gates Motel webgame: http://www.gameslate.com/gatesmotel/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] PHP globals aren't really globals
> Perhaps I'm missing something. In order to access the value of any > variable defined outside the scope of the function, you have to declare > it as "global". This is true in every single instance. If you do not, then > you cannot access the value of that variable. While it's been a while > since I've worked with C, I seem to recall it being the case there as > well. And in Pascal. And in VB. Again, it's been a while so I could be > wrong (but don't think so). Not realy. In most languages, heck , all I can think of, the variable's scope is simply dependant on WHERE it was declared, not how it was declared. If in C/C++ you define a variable outside the scope of any function, then it becomes global and can be accessed by any function. I'm not arguing one side or the other, just stating :) I personally find the PHP way a bit annoiying, but its workable, and i'm SURE there was a reason for it. Just not sure what that reason was :) - John Vanderbeck - Admin, GameDesign > > Chris > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] PHP globals aren't really globals
> I've been programming in C all my life, and now I just started developing > in PHP and I'm really enjoying it, it has all the missing improvements that > C needed to be more user-likely. > > But one thin I can't get, how can PHP call a variabel global, if it isn't > global. > A global var, is a var defined outside all functions and it's available > to all and altered by all, without having to redefine or call the var again. That may be your definition of a global variable. It isn't mine. The fact that you have some way to access the variable from every scope makes it global by my definition. Also, all global variables are available via $GLOBALS['var_name']. And if you really do have 50 global variables, you should really appreciate this feature as your chances of having local/global variable overlaps which can cause weird bugs is that much higher. I would suggest grouping your variables into logical arrays of information and doing a 'global' on these arrays instead. > In PHP, for a var to be global you have to add a 'global $var' inside the > function u want to use it. THis is not nice, what about if u have a form > with 50 fields and want a function to validate all of them, u have to pass > them all to the function or build a little piece of code to make all th > $GLOBALS local right? If you have a form with 50 fields, name them like this: Then simply make do: global $blah > Is this really the idea of global vars? The idea is to avoid really nasty scope-related bugs that are common in C code that uses lots of global variables. Years and years ago when I wrote the first version of PHP I was working for a telco writing software for a large telephone switch. The code was huge and extremely ugly. Global variables everywhere. The team had been chasing a bug for about a week when I got stubborn and decided it was time to kill the bug. I printed out all the source code and laid it out in a long hallway as I crawled along with different coloured pens and manually traced my way through it as none of the debuggers we had at the time were of any use. After countless hours the bug turned out to be inside a function that silently modified a global variable which affected another piece of code in a completely different part of the program. I swore I would not have the same problem in PHP and thus the requirement for people to be explicit about using global variables inside functions. Hopefully it also forces a little bit of structure and organization on people. -Rasmus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] PHP globals aren't really globals
> >Yes. But with regards to form variables, all you need to do is > >make one variable global: > >$HTTP_POST_VARS > >it is an associative array that contains all the post variables from > >the form. Make it global and just loop through it. > exactly, u need to loop to an array to get the globals u want. No, to the the variables/values that you want. > this is not the idea of global, global is global... if it's set outside > of ALL functions it should be available 'with no extra code' > to ALL functions, it works that way in all programming language > I'm familiar with... why not in PHP? that's what I wanna know, > why globals in PHP doesn't really work like globals? Perhaps I'm missing something. In order to access the value of any variable defined outside the scope of the function, you have to declare it as "global". This is true in every single instance. If you do not, then you cannot access the value of that variable. While it's been a while since I've worked with C, I seem to recall it being the case there as well. And in Pascal. And in VB. Again, it's been a while so I could be wrong (but don't think so). Also, PHP was written in C. Why would they institute a behaviour that was radically different than what is part of the parent language? Chris
RE: [PHP] PHP globals aren't really globals
>> function u want to use it. THis is not nice, what about if u >> have a form with 50 fields and want a function to validate >> all of them, u have to pass them all to the function or build >> a little piece of code to make all the $GLOBALS local right? >> Is this really the idea of global vars? > >Yes. But with regards to form variables, all you need to do is >make one variable global: > >$HTTP_POST_VARS > >it is an associative array that contains all the post variables from >the form. Make it global and just loop through it. exactly, u need to loop to an array to get the globals u want. this is not the idea of global, global is global... if it's set outside of ALL functions it should be available 'with no extra code' to ALL functions, it works that way in all programming language I'm familiar with... why not in PHP? that's what I wanna know, why globals in PHP doesn't really work like globals? . [ Christian Dechery ] . Webdeveloper @ Tá Na Mesa! . Listmaster @ Gaita-L . http://www.tanamesa.com.br -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] PHP globals aren't really globals
> function u want to use it. THis is not nice, what about if u > have a form with 50 fields and want a function to validate > all of them, u have to pass them all to the function or build > a little piece of code to make all the $GLOBALS local right? > Is this really the idea of global vars? Yes. But with regards to form variables, all you need to do is make one variable global: $HTTP_POST_VARS it is an associative array that contains all the post variables from the form. Make it global and just loop through it. Chris
Re: [PHP] PHP globals aren't really globals
At 11:24 AM -0300 2/14/01, Christian Dechery wrote: >I've been programming in C all my life, and now I just started developing >in PHP and I'm really enjoying it, it has all the missing improvements that >C needed to be more user-likely. > >But one thin I can't get, how can PHP call a variabel global, if it isn't >global. >A global var, is a var defined outside all functions and it's available >to all and altered by all, without having to redefine or call the var again. > >In PHP, for a var to be global you have to add a 'global $var' inside the >function u want to use it. THis is not nice, what about if u have a form >with 50 fields and want a function to validate all of them, u have to pass >them all to the function or build a little piece of code to make all th >$GLOBALS local right? > >Is this really the idea of global vars? > Well having a form with LOTS of fields, it might be advisable to have the fields send an array straigt away have the name of your fields something like <.. name="feedback[name]" ..> and just use global $feedback and the whole array is at your disposition. This doens't change the way global is treated in PHP but might help you in your setup. having these in an array will also greatly make sessions easier, if you have to follow all these fields on multiple pages. -- Thierry Coopman - [EMAIL PROTECTED] My opinions are personal, and have really nothing or nothing to do with Keytrade! He who laughs last probably made a back-up. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]