Most PHP variables have single scope. They're limited to the context in which they're declared. (A neighborhood analogy.)
Below, PHP creates 3 variables, all named $a.
$a = 0; // this $a has global scope, known only to code outside of functions function x() { $a = 1; // this $a is a new variable in local scope, known only inside x() return ++$a; } function y() { $a = 2; // this $a is known only in y() return ++$a; } echo x(); // yields 2 echo y(); // yields 3 echo $a; // yields 0
Repeated calls to x()
always return 2, since $a is set to the same starting value w/each call.
A function contained inside another function also creates its own local scope. PHP creates 2 separate variables here, too.
function x1() {
$a = 0;
function x2() {
$a = 1; // a new, local var known only inside x2()
}
}
A static variable retains its value between function calls.
function x() { static $a = 1; // $a is initialized only ONE time return ++$a; } echo x(); // yields 2 echo x(); // yields 3 echo x(); // yields 4
("Static" has another use in classes. See classes vs objects.)
Another way to handle persistent values between function calls is to make a global variable visible inside the function.
PHP differs from javascript on this matter. Javascript functions know about global variables. Its functions are like houses with walls made of one-way mirrors. You can see out, but not in.
/* javascript */ var a=1; // a has global scope function go() { a=2; // a refers to the global variable } go(); alert( a ); // produces 2
PHP allows you to do something similar for selected variables through:
When $a is in global scope, $a and $GLOBALS['a'] are synonyms. Superglobals are automatically visible to functions, so these are equivalent:
function go() {
global $a; // $a now refers to the global variable
$a=2;
}
function go() {
$GLOBALS['a']=2;
}
(Only 9 of PHP's predefined variables are superglobal: $GLOBALS, $_SERVER, $_GET, $_POST, $_REQUEST, $_COOKIE, $_SESSION, $_FILES, $_ENV.)
These are also equivalent:
function calc() { global $a, $b, $c; // global can be declared on several variables $a *= 2; $b *= 3; return $a + $b + $c; } function calc() { // this saves 1 line of code but is harder to read $GLOBALS['a'] *= 2; $GLOBALS['b'] *= 3; return $GLOBALS['a'] + $GLOBALS['b'] + $GLOBALS['c']; }
Superglobals are provided for a reason. Given the context in which PHP scripts most often operate, it's useful to have ready access anywhere in your code to server information, form variables, cookies, etc.
Likewise, it's tempting to declare a database connection variable as global, since ready access to the underlying connection is also convenient.
But PHP doesn't predefine names for such connections, so declaring them as global makes the function vulnerable to inconsistent naming elsewhere in the code.
Tethering a function to global variables tends to hobble it and to make it harder to maintain. Usually, you want to pass arguments to functions, instead. The more general a function, the more useful it is. About PHP arguments.