|
PHP. Functions and scope of view, PART 4 |
Static variables. $kartas is assigned every time and destroyed after end of unction. <?
function test(){
$kartas++; echo "cia yra $kartas iskvietimo kartas <br>"; //nieko gero
}
test();//1 test();//1 test();//1
?>If you want to retain the value of a variable between one call to a function and the next, you can declare it as static. And as part of the initial declaration, you can assign a starting value to it. This allows you to test whether you're calling a function for the first time, or for a subsequent time: <?
function test1(){
static $kartas = 0; $kartas++; echo "cia yra $kartas iskvietimo kartas <br>"; //nieko gero
if ($kartas>=3) echo "ir t.t.<br>"; $kartas++;
}
test1();//1 test1();//2 test1();//3
?>
|