<?php
try {
function hello_1(?string $whom): ?string {
if ($whom !== null) {
return "Hello $whom";
}
}
var_dump(hello_1(null));
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}
/*try {
function hello_2(?string $whom): ?string {
if ($whom !== null) {
return "Hello $whom";
}
return;
}
var_dump(hello_2(null));
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}
Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?) in /in/QRj2G on line 20
*/
try {
function hello_3(?string $whom): ?string {
if ($whom !== null) {
return "Hello $whom";
}
return null;
}
var_dump(hello_3(null));
} catch (\Error $e) {
echo $e->getMessage() . PHP_EOL;
}
- Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- hello_1(): Return value must be of type ?string, none returned
NULL
- Output for 7.1.0, 7.2.29 - 7.2.33, 7.3.16 - 7.3.33, 7.4.3 - 7.4.33
- Return value of hello_1() must be of the type string or null, none returned
NULL
- Output for 5.6.0 - 5.6.24, 7.0.0 - 7.0.9
- Parse error: syntax error, unexpected '?', expecting variable (T_VARIABLE) in /in/h6IYF on line 4
Process exited with code 255.
preferences:
141.56 ms | 408 KiB | 5 Q