PHP: Variable with global keyword

This entry is part 10 of 54 in the series PHP Tutorial

In PHP, global variables must be declared global inside a function if they are going to be used in that function.

$a = 1;
$b = 2;

function Sum()
    global $a, $b;

    $b = $a + $b;

echo $b;

The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version.

Series Navigation<< PHP: Constants
PHP: Using $GLOBALS instead of global >>