Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Double Static Variable

<?php

function foo(): int
{
    static $x = 1;
    return $x;
    static $x = 2;
}

echo foo();

WTF PHP…

Addendum: static variables are defined twice, and the second one has precedence. Duplicate static variable definition has been deprecated since PHP 8.3, so it is not possible to do this anymore. Generally speaking, it is recommended to set these definition early in the function, to avoid having the same variable with two different status during execution.

See Also

PHP Error Messages

PHP Features

Last updated: 09 November 2026