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
- Duplicate static variable definition [Try me]
PHP Error Messages
PHP Features
Last updated: 09 November 2026