I am using php 8.0 but for some reason union types and nullable types do not seem to work according to the documentation. ?Type or Type|null should make an argument optional according to the docs (https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.union)
But I get an exception.
8.0.0
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function test(), 1 passed in /var/www/test/test.php on line 10 and exactly 2 expected in /var/www/test/test.php:4
Stack trace:
#0 /var/www/test/test.php(10): test()
#1 {main}
thrown in /var/www/test/test.php on line 4
Simple test code
//function test(string $hello, ?string $world) {
function test(string $hello, string|null $world) {
return $hello . ' ' . ($world ?? 'world');
}
echo phpversion() . PHP_EOL;
// Outputs 8.0.0
echo test('hello') . PHP_EOL;
// Expected output: hello world
echo test('hola','mundo');
// Expected output: hola mundo
What is wrong here?