PHP Questions Home | Register | Login
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website
Unanswered

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

What is a static variable within a function?


A static variable is one that is defined within a function, but the definition only occurs the first time, and then the value can be amended or changed during function calls.

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

difference between fetch_array function & fetch_row function

i want to create a webapp that has 2 fields to fill in with variables there should be 2 random variables to choose from per field there are some meals that both are able to appear in the first and 2nd box if you'd randomize it i want to make a cycle that

I want to use the php include function to bring in a file that is related by $ref to this page and I cannot get it to use the $ref variable. my code line is where echo $ref is the property number basically it is a view of different properties an

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?

1-what is a header("Location:http//www.website.com"); 2-how does it work? 3-What is a reason to use this function?



Custom Search