PHP Return Values (Step-by-Step Guide with Examples)

In PHP, the return statement is used to send a value back from a function to the caller. This makes functions more powerful, as they can process data and return results instead of just printing output.

1. What Is a Return Value in PHP?

A return value is the output a function sends back when it is called. Instead of printing inside the function, you can store the result in a variable for further processing.

Basic Syntax of return Statement

function functionName() {
    return value;
}

2. Returning a Single Value in PHP

Example: Returning an Integer

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

$result = add(5, 10); // Store the return value
echo "Sum: $result";
?>

Output:

Sum: 15

➡ The function returns the sum instead of printing it inside the function.

3. Returning a String from a PHP Function

Example: Returning a String

<?php
function greet($name) {
    return "Hello, $name!";
}

$message = greet("Alice");
echo $message;
?>

Output:

Hello, Alice!

➡ The function returns a custom greeting message.

4. Returning a Boolean Value

Example: Checking Even or Odd

<?php
function isEven($num) {
    return $num % 2 == 0;
}

var_dump(isEven(10)); // true
var_dump(isEven(7));  // false
?>

Output:

bool(true) bool(false)

➡ This function returns true or false, useful in conditional logic.

5. Returning an Array from a Function

Example: Returning Multiple Values Using an Array

<?php
function getUserInfo() {
    return ["Alice", 25, "Developer"];
}

$user = getUserInfo();
echo "Name: $user[0], Age: $user[1], Job: $user[2]";
?>

Output:

Name: Alice, Age: 25, Job: Developer

➡ Arrays allow returning multiple values efficiently.

6. Returning an Object from a PHP Function

Example: Returning an Object

<?php
class Car {
    public $brand;
    public function __construct($brand) {
        $this->brand = $brand;
    }
}

function getCar() {
    return new Car("Toyota");
}

$myCar = getCar();
echo "Car Brand: " . $myCar->brand;
?>

Output:

Car Brand: Toyota

Objects allow complex data structures to be returned.

7. Returning Early Using return

A function stops execution when it encounters a return statement.

Example: Early Return in a Function

 
<?php
function checkAge($age) {
    if ($age < 18) {
        return "Access Denied";
    }
    return "Access Granted";
}

echo checkAge(15); // Output: Access Denied
echo checkAge(21); // Output: Access Granted
?>

➡ The function exits early if age is under 18.

8. Returning Multiple Values Using list() (PHP 7.1+)

Example: Destructuring an Array

<?php
function getCoordinates() {
    return [40.7128, -74.0060]; // Latitude, Longitude
}

list($lat, $long) = getCoordinates();
echo "Latitude: $lat, Longitude: $long";
?>

Output:

Latitude: 40.7128, Longitude: -74.0060

list() extracts multiple return values from an array.

9. Using Type Declarations for Return Values (PHP 7+)

PHP allows return type declarations for better validation.

Example: Enforcing an Integer Return Type

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

echo multiply(4, 5);
?>

Output:

20

➡ The function must return an integer, ensuring data consistency.

10. Returning null in PHP

A function can explicitly return null if no value is found.

Example: Handling Missing Data

<?php
function findUser($id) {
    if ($id != 1) {
        return null; // User not found
    }
    return "John Doe";
}

$user = findUser(2);
echo $user ?? "User not found"; // Use Null Coalescing Operator
?>

Output:

User not found

➡ If null is returned, the default message is displayed.

11. Best Practices for Returning Values in PHP

Use return values instead of echoing inside the function
Use type hints (int, string, array, object) for reliability
Return early (return inside if conditions) for better readability
Use arrays or objects for multiple return values
Avoid modifying global variables inside functions