Page 1 of 1
Why are all my variables global? [solved]
Posted: Mon May 14, 2007 9:52 pm
by nutkenz
Some test code:
Code: Select all
$variable= "BLABLA";
echo"<PRE>";print_r($GLOBALS);echo"</PRE>";
Output (relevant part only):
phpinfo():
Code: Select all
register_argc_argv On On
register_globals Off Off
Posted: Mon May 14, 2007 11:24 pm
by feyd
Your variables are defined in the global scope. Of course they are global.
Posted: Tue May 15, 2007 1:09 am
by Chris Corbyn
Globals are globals, that cannot be changed. $GLOBALS["foo"] is a geniune alternative to accessing $foo in the global namespace. No hacks there. What register_globals does is takes items from the superglobals $_POST, $_GET, $_REQUEST... and adds them to the global namespace directly so $_POST["foo"] is available as just $foo in the global namespace.
Posted: Tue May 15, 2007 2:53 am
by CoderGoblin
Another example to hopefully clarify
Code: Select all
<?php
$myvar1 = 'This is my variable. This is global';
function dummyTest()
{
$myvar2='This is a local variable (scope only in this function)';
echo $myvar1; // Produces no output and a php warning
return $myvar2;
}
echo $myvar1; // prints out as not in function and declared globally
echo $myvar2; // Produces no output and a php warning as the variable is defined in the function and is local to that function.
echo dummyTest(); // prints out the value of $myvar2 as it is returned from the function.
?>
Variable Scope in manual
Posted: Tue May 15, 2007 4:49 am
by nutkenz
It's funny that I didn't know about that...
Thanks everyone.