Solved

How to use global variables

Posted on 2003-10-31
5
210 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
  • 3
5 Comments
 
LVL 6

Accepted Solution

by:
DoppyNL earned 50 total points
Comment Utility
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
Comment Utility

> 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
Comment Utility
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
Comment Utility
@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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
compressing images after upload 4 28
session dropped in IE 10 18
PHP string issue 5 16
Paging Using PHP 7 29
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now