PHP return Keyword
last modified April 16, 2025
The PHP return
keyword is essential for functions and methods. It
terminates function execution and returns a value to the caller. Without return,
functions would not be able to provide results. This tutorial covers all aspects
of using return in PHP.
Basic Definitions
The return
statement immediately ends function execution. It can
return any valid PHP value including arrays and objects. When return is called,
control returns to the calling code.
Functions without explicit return statements return NULL. Return can appear anywhere in a function, but typically comes at the end. Multiple return points are possible but can reduce readability.
Syntax: return expression;
or return;
for void returns.
The expression can be any valid PHP value or variable. Return type declarations
can enforce specific return types.
Basic Function Return
This example shows a simple function that returns a calculated value.
<?php declare(strict_types=1); function addNumbers(int $a, int $b): int { return $a + $b; } $result = addNumbers(5, 3); echo "The sum is: " . $result;
The addNumbers
function takes two integers and returns their sum.
The return type is declared as int. The returned value is stored in $result.
This demonstrates the most basic return usage.
Returning Different Types
This example shows returning different data types from functions.
<?php declare(strict_types=1); function getUserData(string $type) { if ($type === 'name') { return 'John Doe'; } elseif ($type === 'age') { return 30; } elseif ($type === 'active') { return true; } return null; } echo "Name: " . getUserData('name') . "\n"; echo "Age: " . getUserData('age') . "\n"; var_dump(getUserData('invalid'));
The function returns different types based on input. Without strict return types, PHP allows this flexibility. The final return provides a default null value. This shows PHP's dynamic typing capabilities.
Early Return Pattern
This example demonstrates using early returns for validation.
<?php declare(strict_types=1); function processOrder(array $order): ?string { if (empty($order['items'])) { return 'Error: No items in order'; } if ($order['total'] <= 0) { return 'Error: Invalid order total'; } // Process valid order return null; // No error } $result = processOrder(['items' => [], 'total' => 0]); echo $result ?? 'Order processed successfully';
The function checks conditions and returns early if problems are found. This avoids deep nesting of validation logic. The final return indicates success. Early returns make code more readable and maintainable.
Returning Arrays
This example shows returning an array from a function.
<?php declare(strict_types=1); function getCoordinates(): array { return [ 'latitude' => 40.7128, 'longitude' => -74.0060, 'city' => 'New York' ]; } $coords = getCoordinates(); echo "Latitude: {$coords['latitude']}, Longitude: {$coords['longitude']}";
The function returns an associative array with multiple values. Arrays are commonly used to return grouped data. The caller can access array elements individually. This is useful for returning complex data structures.
Return Type Declarations
This example demonstrates strict return type declarations.
<?php declare(strict_types=1); function divide(float $a, float $b): float { if ($b === 0.0) { throw new InvalidArgumentException('Cannot divide by zero'); } return $a / $b; } try { $result = divide(10.0, 2.0); echo "Result: " . $result; } catch (InvalidArgumentException $e) { echo "Error: " . $e->getMessage(); }
The function declares it will return a float value. Attempting to return another type would cause an error. Type declarations make code more predictable. They help catch errors during development rather than runtime.
Returning Objects
This example shows returning an object instance from a function.
<?php declare(strict_types=1); class User { public string $name; public int $age; public function __construct(string $name, int $age) { $this->name = $name; $this->age = $age; } } function createUser(string $name, int $age): User { return new User($name, $age); } $user = createUser('Alice', 25); echo "User: {$user->name}, Age: {$user->age}";
The function creates and returns a User object. Object returns allow for complex data structures with behavior. The caller receives a fully constructed instance. This pattern is common in object-oriented PHP.
Returning by Reference
This example demonstrates returning values by reference.
<?php declare(strict_types=1); function &getCounter(): int { static $counter = 0; return $counter; } $countRef = &getCounter(); $countRef++; echo "Counter: " . getCounter(); // Outputs 1
The function returns a reference to a static variable. Modifying the returned reference affects the original. Reference returns are less common but useful for specific cases. They require careful use to avoid unexpected behavior.
Best Practices
- Consistency: Use consistent return types when possible.
- Clarity: Make return values obvious from function names.
- Documentation: Document return types in PHPDoc comments.
- Validation: Validate data before returning it.
- Simplicity: Avoid complex return structures when possible.
Source
This tutorial covered PHP return statements with practical examples showing various ways to use return in functions and methods.
Author
List all PHP basics tutorials.