1. Local Scope
function Update_counter ()
{
$counter ++;//here $counter as a local variable, not the same as $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 a function to get 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;//declared with the static keyword $counter, with local domain
$counter + +;
echo "Static counter is now $counter \ n";
}
$counter = 10;
Update_counter ();
Update_counter ();
echo "Global counter is $counter \ n";
/* Output:
Static counter is now 1
Static counter is now 2
Global counter is 10
*/
Global and local scopes and static variables in PHP