The static keyword is also used to declare variables
Function which keep their value after the function has ended.
<?php
function add() {
static $count = 0;
$count++;
echo $count;
}
add(); // The Output is : 1
add(); // The Output is : 2
add(); // The Output is : 3
?>
- The static keyword is used to declare the variable $count as static within the
add
() function. - The variable is initialized with a value of 0 the first time the function is called.
- $count is retained from the previous call and incremented by 1.