function increment() { static $count = 0; $count++; echo $count; } increment(); // Output: 1 increment(); // Output: 2 increment(); // Output: 3
class Counter { public static $count = 0; public static function increment() { self::$count++; echo self::$count; } } Counter::increment(); // Output: 1 Counter::increment(); // Output: 2 Counter::increment(); // Output: 3In this example, we declare a static property `$count` and a static method `increment()` inside a class called `Counter`. Whenever `increment()` is called, `$count` is incremented and its value is printed using the `self` keyword to access the static property and method. Package/Library: PHP core language