← Back to PHP Course | Chapter 5: Functions | Lesson 3 of 10

PHP Return Values

A return value is the answer a function hands back, like a vending machine giving you a snack after you pay. The function stops as soon as it gives the answer.
Syntax
php
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
<?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);
?>

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
<?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";
?>

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
<?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);
?>

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
<?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));
?>

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
<?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");
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing code after return, which never runs because return ends the function immediately.
  2. Forgetting return and using the result, so the caller receives null.
  3. Declaring : int and returning a string like "abc", which throws a TypeError.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.