PHP

Most PHP variables have single scope. They're limited to the context in which they're declared. (A neighborhood analogy.)

Imagine your script is an odd little neighborhood. There's a street running through it, lined by parking strips, sidewalks, and houses. There are even recycling bins waiting at the curbs, since you live in a progressive neighborhood.

The odd thing is that the houses have no windows.

Global variables are like the recycling bins along the curb. When you're outside, you can see them all by looking up or down the street. You can open any you like. You can remove or add items as you wish.

Local variables (those declared inside functions) are like containers stored inside the houses. Since the houses lack windows, you must enter a house before you can see what's inside. Once you do, the bins along the curb are out of view.

PHP is a bit like that. Global variables are visible until PHP executes a function. Once PHP enters a function, only the function's local variables are visible.

Once execution leaves the function (walks out of the house) global variables become visible once more.

This is a great convenience. You don't have to invent a unique name for each variable in your program. Its name must only be unique in its current scope.

Note: Scope in Javascript is different than in PHP. In Javascript, a nested (inner) function inherits the scope of its containing (outer) function. Hence, the inner function can see the variables of the outer. And for the same reason, all Javascript functions can see global variables.

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()
	}
}

static

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.)

"global" and superglobals

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'];
}

caveat: arguments vs globals

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.