Return Values in PHP

Last Updated : 24 Oct, 2026

A return value in PHP is the result a function sends back to the code that called it.

  • Use the return statement to send a value back to the caller.
  • The return statement immediately ends the function's execution.
  • Returned values can be stored in variables, reused in expressions, or passed to other functions.
  • If a function does not explicitly return a value, it returns null.

Basic Return Value in PHP

Explain how to use return inside a function.

PHP
<?php
function add($a, $b) {
    return $a + $b;
}

$result = add(10, 20);
echo $result;
?>

Output
30

Returning Different Types of Values

Show that a function can return different types of values, such as integers, strings, arrays, and booleans.

PHP
<?php
function getName() {
    return "Ryan ";
}

function getNumbers() {
    return [10, 20, 30];
}

echo getName();
print_r(getNumbers());
?>

Output
Ryan Array
(
    [0] => 10
    [1] => 20
    [2] => 30
)

Returning Values vs. Printing Values

Explain the difference between return and echo.

PHP
<?php
function calculate() {
    return 5 + 3;
}

$result = calculate();
echo $result * 2;
?>

Output
16
  • return sends a value back to the caller so it can be stored, reused, or processed.
  • echo displays output and does not return that output as the function's result.

Returning Early from a Function

Explain that return immediately stops the function, even when used before the end of its body.

PHP
<?php
function checkAge($age) {
    if ($age < 18) {
        return "Not eligible";
    }

    return "Eligible";
}

echo checkAge(20);
?>

Output
Eligible

Returning null or No Explicit Value

Explain that if a function finishes without executing a return statement, its result is null. A bare return; also returns null.

PHP
<?php
function greet() {
    echo "Hello!";
}

$result = greet();
var_dump($result);
?>

Output
Hello!NULL

Returning Multiple Values

Explain that PHP functions return a single value, but that value can be an array containing multiple results.

PHP
<?php
function getUser() {
    return ["Ryan", 22];
}

[$name, $age] = getUser();

echo "$name is $age years old.";
?>

Output
Ryan is 22 years old.

Introduce return type declarations using : type, including int, string, bool, array, and void

PHP
<?php
function multiply(int $a, int $b): int {
    return $a * $b;
}

echo multiply(4, 5);
?>

Output
20
Comment