Solved

How to use global variables

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

717 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