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

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



ASK A QUESTION

More functions PHP Questions

Hi; I had an email from my hosting company which said that my account was hacked and one script in images folder is trying to send thousands of spams(file name : "/public_html/images/sm5vy7.php"). they blocked my account and asked me to check

Hey, I need a php script to copy a complete directory to a new one (which is a variable). i.e www.webpage.com/example/ --> www.webpage.com/folder_just_created/. I've looked for hours Kurt

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

how can i get the data by using get(),post()methods?

What is a function?



Custom Search