Static Variable in PHP

What is 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.


static varname;

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

Example :

function count()
static $count=0;
return $count;
echo $val1;
echo $val2;

Result :


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.

PHP Topics

Ask Questions

Ask Question