Click to See Complete Forum and Search --> : What, exactly, are global variables?


Archon MkIV
12-06-2005, 07:42 AM
'lo there.

I've been wondering what global variables are, and I'm not referring to sessions.

global varname = "foo";

nickisme
12-06-2005, 07:47 AM
I think (and someone else will probably have a better answer) that they can always be used in ALL classes and functions in a php file

bokeh
12-06-2005, 09:01 AM
I think (and someone else will probably have a better answer) that they can always be used in ALL classes and functions in a php fileActually that is not quite true. All variables are globals if they are created outside a function. Example:<?php
$string = 'value'; // this is a global
?>Example two:<php
$string = 'value'; // this is a global
function GoGoGadget()
{
global $string;
echo $string; // this prints the global "$string";
}
GoGoGadget();
?>Example four:<?php
$string = 'value'; // this is a global
function GoGoGadget()
{
$string = 'Another value'; // now there are two variables named $string, one inside the function and one outside. Both have the same name but contain different values. They are separate in every way.

}
GoGoGadget();
?>

Archon MkIV
12-06-2005, 09:07 AM
Ah, I see, so you quite simply need to adress the variable with "global" to be able to use it in a function or such. Thanks. :)

NogDog
12-06-2005, 09:07 AM
The scope of any variable used within a function is limited to just that function, unless you first declare it as global. Then it refers to the variable of the same name defined outside of that function. For example:

<?php
### example of use of "global" ###
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
<title>Page title</title>
</head>
<body>
<pre>
<?php
function test1()
{
$test = "bar";
}
function test2()
{
global $test; # now we're refering to $test in the main script, below
$test = "bar";
}
$test = "foo";
echo "Initial value: $test\n"; # foo
test1();
echo "After test1(): $test\n"; # foo
test2();
echo "After test2(): $test\n"; # bar
?>
</pre>
</body>
</html>

NogDog
12-06-2005, 09:26 AM
Ah, I see, so you quite simply need to adress the variable with "global" to be able to use it in a function or such. Thanks. :)
Using globals within functions can sometimes lead to problems down the road as scripts get more complex, the problem being that you forget a variable used in one place is also being used in a function somewhere else as a global variable, and you end up changing its value in one place without realizing it has an unwanted effect in another place.

Generally, you should pass variables into a function via its parameter list and return values via the return() statement, and save the use of "global" for the rare situations where you can't get the desired functionality in that manner. For example:

<?php
# passing value through parameter list:
function foo1($bar)
{
$result = ++$bar; # value of $bar only incremented here, not globally
return($result);
}
# using global:
function foo2()
{
global $bar;
$result = ++$bar; # value of $bar is now incremented globally
return($result);
}
$bar = 1;
$test = foo1($bar);
echo "<p>$test</p>"; # 2
echo "<p>$bar</p>"; # 1
$test = foo2($bar);
echo "<p>$test</p>"; # 2
echo "<p>$bar</p>"; # 2
?>