Declaring a variable with a value inside function as static, is will not re-declare or reassign the value when every time that function will called. That is, the last assigned value will remains on that variable.
<?php
function test()
{
static $a = 1;
echo $a;
if ($a <= 10) {
++$a;
test();
}
}
test(); // Will print: 1234567891011
test(); // Will print: 11
?>
Note:
1. We can not use any expression on declaring of static variables. Ex: static $a=5+6; // Is wrong.
2. Declaring a variable as static on outside of function not make sense. Because it will not going to re-call in future, except in some cases like using goto statements.
<?php
function test()
{
static $a = 1;
echo $a;
if ($a <= 10) {
++$a;
test();
}
}
test(); // Will print: 1234567891011
test(); // Will print: 11
?>
Note:
1. We can not use any expression on declaring of static variables. Ex: static $a=5+6; // Is wrong.
2. Declaring a variable as static on outside of function not make sense. Because it will not going to re-call in future, except in some cases like using goto statements.
No comments:
Post a Comment