Home > Article > Backend Development > What are the restrictions on the return values of PHP functions?
The return value of a PHP function can be a scalar, composite, or resource type, but it can only return one value. The return value of a scalar type is coerced to a scalar type. A void function cannot return any value. Functions that do not declare a return value type are assumed to return void.
The return value of the PHP function can be any data type, including:
However, there are some restrictions on the return value type:
Practical case:
The following function is declared to return an integer:
function sum($a, $b): int { return $a + $b; }
This function receives two parameters and returns their sum. If you try to return a string or array, the compiler will report an error:
// 编译器错误 function sum($a, $b): string { return $a + $b; }
Note:
statement should also not return any value. Doing so may result in unpredictable behavior.
The above is the detailed content of What are the restrictions on the return values of PHP functions?. For more information, please follow other related articles on the PHP Chinese website!