๐Ÿงฎ PHP Functions
Estimated reading: 3 minutes 264 views

PHP Returning Values โ€“ Send Data Back from Functions Effectively

Learn how to return values from functions in PHP, from simple scalars to arrays and references, using best practices.


Introduction โ€“ Why Return Values Matter

In PHP, the return statement is used to send data back from a function to wherever it was called. It allows your functions to be reusable, testable, and chainable by passing results into other operations or functions.

In this guide, youโ€™ll learn:

  • How to return values from PHP functions
  • Returning scalars, arrays, and references
  • Using return types (PHP 7+)
  • Best practices for function design and readability

Basic Return Statement

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

$result = add(3, 5);
echo $result; // 8

return stops the function and sends the result to the caller.


Returning Arrays

function getUser() {
    return ["name" => "Alice", "email" => "alice@example.com"];
}

$user = getUser();
echo $user["name"]; // Alice

Use array return for multiple related values.


Returning Objects

function getDateTime() {
    return new DateTime();
}

$dt = getDateTime();
echo $dt->format("Y-m-d");

Functions can return any type, including objects and class instances.


Returning by Reference

function &getCounterRef(&$val) {
    return $val;
}

$num = 10;
$ref = &getCounterRef($num);
$ref = 20;

echo $num; // 20

Use & to return a reference instead of a copy (used sparingly).


Return Type Declarations (PHP 7+)

function multiply(int $x, int $y): int {
    return $x * $y;
}

echo multiply(2, 4); // 8

Enforces that the function returns an integer. Will cause a fatal error if violated.


No Return โ€“ Returns null

function doNothing() {}

$result = doNothing();
var_dump($result); // NULL

If no return is used, PHP automatically returns null.


Best Practices

  • Always use return for reusable logic
  • Use meaningful return types (array, object, scalar)
  • Declare return types in PHP 7+ for type safety
  • Avoid returning references unless absolutely necessary
  • Keep return values predictable (donโ€™t mix return types)

Summary โ€“ Recap & Next Steps

The return keyword in PHP allows functions to send results back to the caller, enabling chained operations, conditional logic, and cleaner separation of concerns.

Key Takeaways:

  • Use return to output values from a function
  • You can return strings, numbers, arrays, objects, or references
  • Functions without a return will return null
  • Type declarations improve reliability and readability

Real-World Use Cases:
Calculators, data lookups, form validation results, user sessions, reusable data utilities.


Frequently Asked Questions (FAQs)

Can I return multiple values from a function?
Yes. Use an array:

return [$a, $b];

What happens after a return is executed?
The function ends immediately, and no further code runs.

Can I return by reference in PHP?
Yes, using &, but it should be avoided unless needed for optimization.

What if I donโ€™t use return?
PHP automatically returns null.

Are return type declarations mandatory?
No, but theyโ€™re highly recommended in PHP 7+ for stricter code.


Share Now :
Share

๐ŸŽฏ PHP Returning Values

Or Copy Link

CONTENTS
Scroll to Top