1. Local scope
function update_counter()
{
$counter++;//$counter here is a local variable, which is different from the $counter outside the function
}
$counter = 10;
update_counter();
echo $counter;
//Output: 10
2. Global scope
function update_counter()
{
global $counter;//Use the global keyword to declare within the function to obtain the $counter of the global domain
$counter++;
}
$counter = 10;
update_counter();
echo $counter;
//Output: 11
function update_counter()
{
$GLOBALS[counter]++;
}
$counter = 10;
update_counter();
echo $counter;
//Output: 11
3. Static variables
function update_counter()
{
static $counter = 0;//Use the static keyword to declare $counter, which has a local domain
$counter++;
echo "Static counter is now $countern";
}
$counter = 10;
update_counter();
update_counter();
echo "Global counter is $countern";
/*Output:
Static counter is now 1
Static counter is now 2
Global counter is 10
*/