|
Posted by ZeldorBlat on 11/03/05 21:55
First of all, in the example you provided, myvar is a constant, not a
variable. So if you ever try to get to it with $myvar you'll likely
get some sort of error or warning.
Constants and variables behave differently when looking at them from
another scope. When you define a constant in the global scope (that
is, outside of a class or function), it is available
everywhere...period. When you define a variable in the global scope,
it is available in the global scope as you would expect.
However, when you are in a different scope, such as a function or
class, global variables won't just "show up" automatically. You can
get to it, however, using either of the following methods:
//First method:
global $myvar; //$myvar is the global myvar
$x = $myvar; //$x gets the value from the global variable "myvar"
//Second method:
$x = $_GLOBALS['myvar']; //$x gets the value from the global variable
"myvar"
While I'm not entirely positive, I believe that the first one makes the
local $myvar a *reference* to the global $myvar. In other words, if
you change the value of $myvar from within that function and return to
the global scope, you'll find that $myvar has the new value.
If myvar is a constant, you can get to him from anywhere (the script, a
class definition, or a function) by just writing:
$x = myvar; //no $ since myvar is a constant
Navigation:
[Reply to this message]
|