How to use global variables

I need to use global variables that will be visible in functions that are in code.
Here is an example

global $var1 = 2;
function foo()
{
  echo $var1;
}
foo();
echo "b";
?>

All it does is respond "b".
webservjavaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DoppyNLCommented:
using global variables is generally a bad idea, as that could result in unexpected results, this is how you should actually do it:
---------
$var1 = 2;
function foo($myvar)
{
   echo $myvar;
}
foo($var1);
---------
But if you *really* have to because of some obscure reason, you best use this:
---------
$GLOBALS['var1'] = 2;
function foo()
{
    echo $GLOBALS['var1'];
}
foo();
-----------
or:
$var1 = 2;
function foo()
{
    global $var1;
    echo $var1;
}
foo();
-------------
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Giovanni GCTOCommented:

> But if you *really* have to because of some obscure reason, you best use this:
> ---------
> $GLOBALS['var1'] = 2;
> function foo()
> {
>     echo $GLOBALS['var1'];
> }
> foo();

the first $GLOBALS['var1'] = 2; is just confusing, change it to:
$var1 = 2;
because you are in the global scope. :-)
0
DoppyNLCommented:
I know it is a little confusing.
I use it like this in my own code for some information needed acros the code.
The reason I put it like that is to show the difference between the global once's and the local one's.
Make's it a bit more easy for me to read it :)
0
DoppyNLCommented:
@webservjava:

btw, you've still got an old question open:
http://www.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20664496.html

Don't forget to close this one as well when you get an answer that suits your needs.
0
shmertCommented:
I like doppy's code of using $GLOBAL to set the variable, even if you're already in the global scope.  It makes it clear that the variable will be referenced in the global scope somewhere else, which is good info to have, since it probably won't be immediately apparent what the variabel does, and could otherwise get deleted during code cleanup.

It's good practice to name global variables as all uppercase, this further distinguishes them as being global variables.

One last note about global declaration:   There are times when you _think_ you are in global scope, but that may not always be the case.  For example, if you have a class that includes a file in one otf its methods.  Then setting the $GLOBALS['foo'] = 123; as Doppy did will have the desired behavior.

Nit-picky points, maybe, but I've just run into an obstacle trying to implement 3rd-party apps, and global vars are giving me plenty of trouble.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.