Return Values
The return statement is how a function hands a result back to whatever called it. As soon as PHP executes a return, the function stops running immediately — any code written after it in the same branch is dead code and will never run. Understanding exactly when and what a function returns is one of the most practical things you can learn about PHP, because it affects everything from how you structure conditionals to what var_dump() shows you when something looks like it "returned nothing."
return exits immediately
return does not just supply a value — it is also a control-flow statement. The moment it runs, PHP leaves the function, skipping every remaining line, loop iteration, or echo that would otherwise follow.
Code after return never executes
<?php
function checkAge(int $age): string
{
if ($age < 18) {
return "minor";
}
echo "This line never runs for age 15\n";
return "adult";
}
echo checkAge(15);
echo "\n";
echo checkAge(25);minor adult
Guard clauses: early returns instead of deep nesting
Because return exits right away, you can use it to handle invalid or edge cases at the top of a function and return early, leaving the rest of the function to deal only with the "normal" case. This pattern is called a guard clause, and it usually reads far better than wrapping the entire function body in nested if blocks.
Guard clauses flatten nested conditionals
<?php
// Without guard clauses - nesting grows with every rule
function calculateDiscountNested(float $price, bool $isMember): float
{
if ($price > 0) {
if ($isMember) {
return $price * 0.9;
} else {
return $price;
}
} else {
return 0.0;
}
}
// With guard clauses - each rule exits early, no nesting
function calculateDiscount(float $price, bool $isMember): float
{
if ($price <= 0) {
return 0.0;
}
if (!$isMember) {
return $price;
}
return $price * 0.9;
}
echo calculateDiscount(100.0, true);
echo "\n";
echo calculateDiscount(100.0, false);
echo "\n";
echo calculateDiscount(-5.0, true);90 100 0
Both versions of calculateDiscount() compute the same thing, but the guard-clause version keeps the "happy path" — the discount calculation — at the lowest indentation level, and pushes the exceptional cases (invalid price, non-member) to the top where they are handled and immediately forgotten about.
Returning multiple values with an array
PHP functions can only return a single value directly, but that value is very often an array, which lets you bundle several results together. On the caller's side, list destructuring with [$a, $b] = someFunction() (or the older list($a, $b) = someFunction() syntax) unpacks that array straight into separate variables.
Returning min and max together
<?php
function minMax(array $numbers): array
{
return [min($numbers), max($numbers)];
}
[$low, $high] = minMax([4, 19, 2, 87, 33]);
echo "Low: {$low}, High: {$high}";Low: 2, High: 87
You can also destructure an associative array by matching keys, which is often clearer than relying on positional order because the caller does not need to remember which slot holds which value.
Destructuring by key
<?php
function parseName(string $fullName): array
{
[$first, $last] = explode(" ", $fullName, 2);
return ["first" => $first, "last" => $last];
}
["first" => $firstName, "last" => $lastName] = parseName("Grace Hopper");
echo "{$firstName} / {$lastName}";Grace / Hopper
The old list() syntax
Before the short [$a, $b] = ... syntax existed, PHP used list($a, $b) = ... for the same purpose. Both forms are still valid today and behave identically; list() simply predates the square-bracket shorthand and shows up often in older codebases.
list() is equivalent to the square-bracket form
<?php
function divide(int $a, int $b): array
{
return [intdiv($a, $b), $a % $b];
}
list($quotient, $remainder) = divide(17, 5);
echo "{$quotient} remainder {$remainder}";3 remainder 2
No return statement means null
If a function's execution path never reaches a return statement — either because there simply isn't one, or because none of the conditional branches that contain one were taken — the function implicitly returns null. This is easy to overlook because nothing errors; the caller just silently gets null back.
A function with no return statement
<?php
function logAndForget(string $message)
{
echo "LOG: {$message}\n";
// no return statement here
}
$result = logAndForget("Something happened");
var_dump($result);LOG: Something happened NULL
return;with no value is equivalent toreturn null;— both exit the function and hand backnull.A
voidreturn type declaration documents that a function is not meant to return a meaningful value at all (covered on the Type Declarations page).Destructuring assignment (
[$a, $b] = ...) silently assignsnullto any variable whose position or key is missing from the returned array — it will not throw an error.returninside a loop exits the entire function, not just the loop; usebreakorcontinueif you only want to affect the loop.