➤ Normally, when a function is completed/executed, all of its variables are
deleted. However, sometimes we want a local variable NOT to be deleted.
We need it for a further job.
• To do this, use the
static
keyword when you first declare the variable:
For instance:
// function with static variable
function get_num1() {
static $number1 = 0;
echo $number1++." ";
}
get_num1();
get_num1();
get_num1(); echo "\n";
// outputs: 0 1 2
// function without static variable
function get_num2() {
$number2 = 0;
echo $number2++." ";
}
get_num2();
get_num2();
get_num2();
// outputs: 0 0 0