Declaring the same static variable multiple times within the same scope will result in a Fatal Error since PHP 8.3. Example:

function example_function() {
    static $counter = 0;  // First declaration of static variable

    $counter++;

    if ($counter > 3) {
        static $counter = 0;  // Second declaration of static variable, which causes the error
    }
}

// Calling the function multiple times
example_function();
example_function();

PHP 8.3 Fatal error: Duplicate declaration of static variable
Tagged on:

Sanjeev Aryal

Don't bury your thoughts, put your vision into reality ~ Bob Marley.

Leave a Reply

Your email address will not be published. Required fields are marked *