← CodePrimer
PHP
Intermediate
Basics
Static variable in a function
How do I keep a value between function calls?
static $count.
<?php
function hits() {
static $count = 0;
$count++;
return $count;
}
echo hits(); // 1
echo hits(); // 2
Notes
Static preserves value across calls in the same request.
Keywords: static · scope · function
Related
HTML
HTML headings h1–h6
How do I create page headings?
CSS
Three ways to apply CSS
What are inline, internal, and external CSS?
PHP
Embed PHP inside HTML
How do I mix PHP and HTML?
HTML
Paragraphs and line breaks
How do I break text into paragraphs?
CSS
CSS box model
Why is my element wider than expected?
PHP
PHP comments
How do I write comments in PHP?