PHP Return Values
In this page:
function function_name($parameter): return_type {
return $value;
}
$result = function_name($argument);
Returning Values
return sends a value back to whatever code called the function, and execution of the function stops the instant return runs — any code written after it inside the same function never executes.
Example: Returning Values
<?php
// Define the function `add` taking `$a`, `$b`
function add($a, $b) {
// Return `$a + $b` from this function
return $a + $b;
// Print "This never runs" to the output
echo "This never runs";
}
// Print `add(2, 3)` to the output
echo add(2, 3);
?>
Login to try C/C++/Java/PHP code in the editor
Returning Multiple Values
A PHP function can only return one value directly, but you can bundle several related values into an array and return that instead — then unpack the array back into separate variables at the call site using list() or square-bracket destructuring.
Example: Returning Multiple Values
<?php
// Define the function `getName` with no parameters
function getName() {
// Return `["Alice", "Smith"]` from this function
return ["Alice", "Smith"];
}
[$first, $last] = getName();
// Print "$first $last" to the output
echo "$first $last";
?>
Login to try C/C++/Java/PHP code in the editor
Return Type Declarations
Adding : type after a function's closing parenthesis declares what type its return value must be.
If the function's body tries to return something that doesn't match, PHP raises a TypeError — this catches a whole class of bugs at the source rather than wherever the mismatched value eventually causes trouble.
Example: Return Type Declarations
<?php
// Define the function `add` taking `int $a`, `int $b`
function add(int $a, int $b): int {
// Return `$a + $b` from this function
return $a + $b;
}
// Print `add(2, 3)` to the output
echo add(2, 3);
?>
Login to try C/C++/Java/PHP code in the editor
Nullable Return Types
Prefixing a return type with ? (function find(): ?User) marks it nullable, explicitly allowing the function to return either the declared type or null.
Without that ?, returning null from a function typed to return User would trigger a type error.
Example: Nullable Return Types
<?php
// Define the function `findUser` taking `bool $exists`
function findUser(bool $exists): ?string {
// Return `$exists ? "Alice" : null` from this function
return $exists ? "Alice" : null;
}
// Print a detailed dump (with types) of `findUser(false)`
var_dump(findUser(false));
?>
Login to try C/C++/Java/PHP code in the editor
Void Return Type
A void return type is PHP's way of stating a function performs an action but deliberately produces no value to hand back — logging a message, say, or updating a database row.
Trying to return an actual expression from inside a void function is a compile-time error, not just a style violation.
Example: Void Return Type
<?php
// Define the function `logMessage` taking `string $msg`
function logMessage(string $msg): void {
// Print "LOG: $msg" to the output
echo "LOG: $msg";
}
// Call `logMessage("Saved successfully")`
logMessage("Saved successfully");
?>
Login to try C/C++/Java/PHP code in the editor
- Writing code after
return, which never runs becausereturnends the function immediately. - Forgetting
returnand using the result, so the caller receivesnull. - Declaring
: intand returning a string like"abc", which throws aTypeError.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: