PHP 8
Intermediate
Basics
· 1 min read
Static variable in a function
How do I keep a value between function calls?
static $count.
Example
<?php
function hits() {
static $count = 0;
$count++;
return $count;
}
echo hits(); // 1
echo hits(); // 2
Line-by-Line Walkthrough
Static preserves value across calls in the same request.