Home >>PHP Object Oriented >PHP Static Variables

PHP Static Variables

Static Variable

When a function is completed, all of its variables are normally deleted.
<?php        
function Test()        
{        
$x=0;        
echo $x;        
$x++;        
}            
Test();        
Test();        
Test();    
?>
 Output 0 0 0
However, sometimes you want a local variable to not be deleted then use static keyword.
<?php        
function Test()        
{        
static $x=0;        
echo $x;        
$x++;        
}            
Test();        
Test();        
Test();    
?>
 Output 0 1 2

No Sidebar ads