Optional Parameter Are After Compulsory Parameters¶
Optional parameters have a default value. When running the functioncall, PHP assigns the parameters by position. This way, the first parameter would get the value, even though it has the default value, and then, there will be a missing argument for the second one.
Since PHP 8.0, PHP reports that situation. It might be turned into an error in PHP 9.0
PHP code¶
<?php
function foo($a = 1, $b) {
print $a $b\n;
}
foo(1, 2);
?>
Before¶
PHP Deprecated: Required parameter $b follows optional parameter $a
Deprecated: Required parameter $b follows optional parameter $a
1 2
After¶
PHP Deprecated: foo(): Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter
Deprecated: foo(): Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter
1 2
PHP version change¶
This behavior changed in 8.0