Variable scope is a boundary for a variable within which it can be visible or accessed from code.
There are only two scopes available in PHP namely local and global scopes.
- Local variables (local scope)
- Global variables (special global scope)
- Static variables (local scope)
- Function parameters (local scope)
function printHW(){
global $hw; //global scope
$name = "nahid"; //local scope
echo $hw;
}
function printHW2(){
echo $GLOBALS['hw']; //$GLOBALS -> Super Global Array
}
#STATIC SCOPE
function doSomething() {
static $i;//static scope
$i = $i?? 0;
$i++;
echo $i;
echo "\n";
}
doSomething();
doSomething();
doSomething();