← CodePrimer
PHP Intermediate Basics

Static variable in a function

How do I keep a value between function calls?

static $count.

Sample
<?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

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share