?
Solved

How to use global variables

Posted on 2003-10-31
5
Medium Priority
?
224 Views
Last Modified: 2008-03-06
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".
0
Comment
Question by:webservjava
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 6

Accepted Solution

by:
DoppyNL earned 200 total points
ID: 9656158
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
 
LVL 14

Expert Comment

by:ThG
ID: 9656255

> 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
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9656263
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
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9656274
@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
 
LVL 11

Expert Comment

by:shmert
ID: 9659501
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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question