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 in /codes/OptionalParameterLast.php on line 3
Deprecated: Required parameter $b follows optional parameter $a in /codes/OptionalParameterLast.php on line 3
1 2
After¶
PHP Deprecated: foo(): Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter in /codes/OptionalParameterLast.php on line 3
Deprecated: foo(): Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter in /codes/OptionalParameterLast.php on line 3
1 2
PHP version change¶
This behavior changed in 8.0