I'm sure others have already thought of this and done it (it's a very short and easy method) but I figured I'd post it for others. I got annoyed with always running into errors when working on my site when I'd forget to global a string in a function that I recently created and such. Gets tedious having to global all of them each time in every function. Anyways, I made a very short function that will help with this (I'm gonna feel stupid if there's already a built-in one for it, but I doubt it). function CreateGlobals() { #THIS FUNCTION WILL EASILY CREATE A LINE OF CODE #TO GLOBAL EVERY VARIABLE THAT CURRENTLY EXISTS #IN THE GLOBAL SCOPE SO THAT IT CAN BE USED IN #THE LOCAL SCOPE WHERE YOU CALL THIS FUNCTION #USAGE: eval(CreateGlobals()); return 'global $' . implode(array_keys($GLOBALS), ',$') . ';'; } PHP: All you would then have to do is this: $var = 'blah blah'; function YourFunction() { eval(CreateGlobals()); print $var; } PHP: It would print 'blah blah' though it wouldn't normally unless you manually typed "global $var;" at the top of the function. Of course, it will also global the ones you don't even need global'd (such as $GLOBALS itself since it's recursive, and other already-global ones like $_GET and $_POST). It's useful nonetheless though.
Gross. The use of globals at all is such a bad idea. You eliminate half the value of having functions in the first place. Please don't do this. Write your functions correctly such that they do independent work and pass in the parameters that are required.
True in some cases if you have a bunch of vars that you might accidentally overwrite in your script but this, on my site, is very useful since I have a lot of classes that get used in a lot of other functions and I'm constantly adding new ones and such (I'm not going to pass each class as a parameter to every function). I name them properly though so that I don't accidentally create the same one inside a function and overwrite the original. That's the one thing you have to beware of.
And there's nico_swd to get me, aha. Though, back on the other subject, I don't think this eliminates the point of functions. One of the main points of a function is so that you can keep calling the same code over and over without having to actually have the same code multiple times in your script. It's useful to be able to touch all the outside variables from inside them. EDIT: Wait, nico, wouldn't that just create variables in the function with those names? If you change the values, it won't change the global ones I assume? Another point of this is being able to alter those variables too, though if you don't want to alter them, yours is perfect. I don't mean alter them all, but I have several variables that are meant to be added onto (such as arrays).
There's nothing wrong with refering to a variable that lives in the global scope from within a function. However getting too lazy and setting up automated extractions like this is what leads to security holes.
Yes a function call is better than copying chunks of code. But you're still missing the point of functions. A function should be a discreet independent chunk of code that doesn't rely on globals. There most certainly is something wrong with using globals within a function. There are times when avoiding globals is so much effort it's not worth it but those are the exception. Remember a well written function can be used in another context. If your function is referencing globals it cannot be used in other contexts. What you're doing when you use functions this way is simply a convoluted form of using includes.
I didn't say you had to use this in every one of your functions but there can be ones where you do want to access the globals. There are many functions where I do not use this, so I'm not completely crazy. I guess you'd have to see the structure of our site which I obviously won't show because it's all private.
since I'm into OOP, I need to use globals all the time.... $site = new Site(); $site->htmlhead = new Head(); PHP: and inside html head would look like.... class Head { public function Draw() { global $site; $site->whatever_here;................... PHP: