How do I get a variable in a function to retain its value between calls?
<?php
function myfunc() { static $myfunctionvariable = 1;
echo "the number $myfunctionvariable\n";
$myfunctionvariable++;
}
myfunc(); //1
myfunc();//2
myfunc();//3
?>
The use of the static keyword means that it will only be assigned the value 1 once, so each time the function is called the number will increment by one. If you do not use the static keyword, each time the function is called the variable will be set back to 1 each time.
Copy the code and try calling the function a few times both with and without the static keyword to see the effect it has.
Comment on this Question and Answer >>>
ASK A QUESTION
More functions PHP Questions
what is var_dump function in php?How can I call online a PHP function. It works local but when I put it online it does nothing. I call online a php function to load pictures into my flex application. Thanks Bart
Is there a way to use readfile() and not have it display the byte number at the bottom?
I have used the funtion Rate (code below) and i have some troubles with it. When i use it with the following values: (Wrong) Excel - RATE(228;-196,02;-49005;961546,464646;1;0,05) gives: 0,0119 Php - RATE(228,-196.02,-49005,961546.464646,1,0.05) gives
how to create a search engine
