Solved

How to use global variables

Posted on 2003-10-31
5
222 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 50 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JQuery Search Filter 2 71
Need guidance for special auto increment values 2 22
Multi line FPDF footer: 3 28
Update Set Column Query Not Working as Expected 6 9
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 is …
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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