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

Static Variable Accepts Functioncalls As Default

Static variables are actually variables: as such, they can be inited with the result of a functioncall.

Until PHP 8.3, their default values were using static constant expression, built around constants and operators.

Since PHP 8.3, it is possible to also set their first value as a function or method call.

Properties and parameters are not allowed to use these expressions: they must be valid at compile time.

PHP code

<?php

function foo() {
	static $x = goo(1);
	
	return ++$x;
}

function goo() {
	return 3;
}

echo foo();
echo foo();

?>

Before

PHP Fatal error:  Constant expression contains invalid operations

Fatal error: Constant expression contains invalid operations

After

45

PHP version change

This behavior changed in 8.3.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat