A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function.
A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:
<?php
$name='GURAM';//Global variable
function local_var() {
$name='guram'; //local variable
echo 'Local variable: '.$name ."\n";
}
local_var();
echo 'Global variable: '.$name;
?>
"static"
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job. To do this, use the static keyword when you first declare the variable:
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>