PHP: Define function that returns multiple values (3 ways)

Updated: January 10, 2024 By: Guest Contributor Post a comment

Overview

PHP is a server-side scripting language that is used extensively for web development. One of the strengths of PHP is its simplicity and ease of use, particularly when it comes to functions. In this tutorial, we’re going to explore a more advanced aspect of functions in PHP: defining a function that can return multiple values.

Understanding how to return multiple values from a function in PHP is particularly useful in scenarios where you need to perform a series of calculations or retrieve several bits of data in one go, avoiding the need to invoke multiple functions. It streamlines your code and can make it easier to maintain and understand.

Understanding the Basics

Before we dive into the multitasking aspect of PHP functions, let’s review the fundamental concept of a PHP function. A function is a reusable block of code that you can call from other parts of your script to perform a specific task. Standard PHP functions return one value using the return statement. However, PHP doesn’t support returning multiple values directly. Instead, we use other structures like arrays or objects to simulate this behavior.

Method 1: Returning an Array

The most straightforward method to return multiple values from a PHP function is by using an array. When your function performs various operations and you need to return more than one result, you simply put the results in an array and return the array.

Example:

function calculateDimensions($length, $width, $height) {
  $volume = $length * $width * $height;
  $surfaceArea = 2 * ($length * $width + $width * $height + $height * $length);

  return array('volume' => $volume, 'surfaceArea' => $surfaceArea);
}

// Using the function
$dimensions = calculateDimensions(5, 3, 2);
echo 'Volume: ' . $dimensions['volume'];
echo 'Surface area: ' . $dimensions['surfaceArea'];

This example defines a function calculateDimensions that calculates and returns the volume and surface area of a rectangular prism.

Method 2: Using list() to Extract Values

PHP provides a convenient function called list() that you can use to assign variables in one operation to the elements of an array. This can make the code cleaner when you’re working with an array returned by a function.

Example:

list($volume, $surfaceArea) = calculateDimensions(5, 3, 2);
echo 'Volume: ' . $volume;
echo 'Surface area: ' . $surfaceArea;

Using list(), the values returned from calculateDimensions are immediately assigned to $volume and $surfaceArea.

Method 3: Returning an Object

If you want to return multiple values that are related to each other, you might want to consider returning an object from your function, particularly if these values represent properties of a single conceptual entity.

Example:

function calculateDimensions($length, $width, $height) {
  $result = new stdClass();
  $result->volume = $length * $width * $height;
  $result->surfaceArea = 2 * ($length * $width + $width * $height + $height * $length);

  return $result;
}

// Using the function
$dimensions = calculateDimensions(5, 3, 2);
echo 'Volume: ' . $dimensions->volume;
echo 'Surface area: ' . $dimensions->surfaceArea;

stdClass is a generic empty class in PHP that you can use to create an object on the fly. Here, we’re assigning the volume and surfaceArea as properties of this object. You can then access these properties using the arrow operator (->).

Additional Tips and Considerations

There are a few things to keep in mind when returning multiple values from a PHP function. First, while using arrays or objects is convenient, it may obscure what the function is returning, especially if the function’s documentation is unclear. Always document your functions well to indicate what type of values they return, and in what structure.

Another point to consider is that if you’re using objects to return multiple values, it’s generally better to create a specific class for that purpose rather than using stdClass. This can make your code more self-documenting and help with auto-completion features in IDEs.

Lastly, be wary of the performance implications if you find yourself needing to frequently return a large number of values from your functions, especially in a busy web application. Each approach has its own overhead, and in some cases, you may need to review your application’s architecture for a more efficient data management strategy.

Conclusion

In this tutorial, we’ve seen how to define functions in PHP that return multiple values, which is not directly supported in PHP, but can be accomplished using arrays and objects. This can greatly improve the structural organization and readability of your code. Hope you found this guide useful in your PHP development journey.