What are the potential pitfalls of using a global variable to store the counter value in PHP?

Using a global variable to store the counter value in PHP can lead to potential issues with variable scope and unintended modifications of the counter value by other parts of the code. To solve this issue, you can encapsulate the counter variable within a class and use class methods to manipulate the counter value, ensuring better control over its access and modification.

class Counter {
    private static $count = 0;

    public static function increment() {
        self::$count++;
    }

    public static function decrement() {
        self::$count--;
    }

    public static function getCount() {
        return self::$count;
    }
}

Counter::increment();
echo Counter::getCount(); // Output: 1