What is a static variable within a function?
Thus if you wanted to count how many times a function was called, you would declare the variable as a static variable with the 'static' keyword like this:
<?php
function nothing() { static $callme = "1"; echo "I have $callme things to declare\n"; $callme++; }
for($i =0; $i < 10; $i++) { nothing(); }
?>
Prints this:
I have 1 things to declare
I have 2 things to declare
I have 3 things to declare
I have 4 things to declare
I have 5 things to declare
I have 6 things to declare
I have 7 things to declare
I have 8 things to declare
I have 9 things to declare
I have 10 things to declare
If you remove the keyword 'static' then you get this instead:
I have 1 things to declare
I have 1 things to declare
I have 1 things to declare
I have 1 things to declare
I have 1 things to declare
I have 1 things to declare
I have 1 things to declare
I have 1 things to declare
I have 1 things to declare
I have 1 things to declare
Comment on this Question and Answer >>>
ASK A QUESTION
More functions PHP Questions
WHAT IS THE CORRECT WAY TO OPEN THE FILE "time.text" AS READABLE?which function can assign the output to a string variable?
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
PHP Fatal error does not result in HTTP error code 500. When PHP code throws a Fatal error, I would expect Apache to respond with HTTP 500, not a 200?
How do I make a variable from outside a function work in that function?
