Tuesday, 20 January 2015

Static Variable in PHP

PHP Static variable is declared using a keyword "static". It remains in local scope but retains the value till the program execution completes.

Syntax:


static varname;

In the above syntax, static is a keyword and varname is the variable name.

Example:


<?php
function count()
{
static $count=0;
$count++;
return $count;
}
$val1=count();
$val2=count();
echo $val1;
echo $val2;
?>

Result


1
2

In the above example, the variable $count is declared as static variable. So, it won't be initialized each time when the function count() is called.

0 comments:

Post a Comment