What is the best way to implement a counter variable for displaying sequential numbers in PHP?

To implement a counter variable for displaying sequential numbers in PHP, you can use a static variable within a function that gets called each time you need to display the next number. This static variable will retain its value between function calls, allowing you to increment it each time the function is called.

function displaySequentialNumber() {
    static $counter = 1;
    echo $counter;
    $counter++;
}

// Call the function to display sequential numbers
displaySequentialNumber(); // Output: 1
displaySequentialNumber(); // Output: 2
displaySequentialNumber(); // Output: 3