Returning two values from a function

Returning two values from a function

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Returning Multiple Values from Functions in PHP

You cannot return two distinct values directly from a single return statement in PHP using multiple variables like return $uid, $sid;. However, you can absolutely achieve this functionality by leveraging specific language features to encapsulate those values. The example you provided demonstrates a common misconception: when a function hits a return statement, execution halts immediately for that scope. Consequently, the second return $sid; line in your snippet is unreachable code and will never execute.

To return multiple pieces of data, PHP offers two primary idiomatic patterns: returning an array or using named tuples (introduced in PHP 8.0).

Returning Arrays

The most traditional and universally compatible approach is to return a single array containing all the desired values. In your specific case, you can modify the function to build an associative array. This allows you to distinguish between the ID and the SID later, rather than relying on positional indices which are prone to errors if you change the order of arguments.

function ids($uid = 0, $sid = '')
{
    // Simulating logic where uid might be passed or calculated
    if ($uid === 0) {
        $uid = 1;
    }
    
    if (empty($sid)) {
        $sid = md5(time());
    }

    return [
        'id' => $uid,
        'sid' => $sid
    ];
}

// Usage
$result = ids();
echo $result['id']; // Outputs: 1
echo $result['sid']; // Outputs: The generated SID

This approach is robust because it decouples the retrieval of values from their order in the return statement. If you decide later that the uid calculation should happen after the sid generation, your calling code remains unaffected. This pattern scales well when returning more than two values or complex objects alongside primitives.

Returning Named Tuples (PHP 8+)

If you are targeting PHP 8.0 or higher, you can utilize named tuples, a feature designed specifically for this scenario. This syntax allows you to return multiple variables in a single expression and retrieve them by name immediately upon assignment. It provides a cleaner syntax than arrays when the data structure is simple and intended for immediate unpacking.

function ids($uid = 0, $sid = '')
{
    if ($uid === 0) {
        $uid = 1;
    }
    
    if (empty($sid)) {
        $sid = md5(time());
    }

    return [$uid, $sid]; // Returns a named tuple
}

// Usage
[$id, $sid] = ids();

echo $id;      // Outputs: 1
echo $sid;     // Outputs: The generated SID

However, if you want to access them by name directly after unpacking or without immediate unpacking logic, you can rely on the properties added to the tuple. While standard array access still works, the named tuple syntax makes the intent explicit: these are paired values meant to be consumed together.

Choosing the Right Approach

The decision between returning an array and a named tuple often comes down to version constraints and data complexity. If you need to maintain compatibility with PHP 7.x or lower, the array approach is the only option. Furthermore, if your function might return null for one of the values under specific error conditions, an associative array handles null gracefully without breaking the structure, whereas named tuples can sometimes behave unexpectedly if not handled carefully in older environments.

There is also a consideration regarding how these values are used downstream. If you are building a Laravel application or using a framework that heavily utilizes dependency injection, returning a single object or a structured array is often preferred for type hinting clarity. You can even create a dedicated class to hold these two values, ensuring that the interface between your function and the rest of your application remains stable even if the internal logic changes.

In summary, while you cannot chain return statements, wrapping your values in an array or utilizing named tuples provides a clean, efficient solution. This ensures that your function behaves predictably regardless of how many times it is called or what arguments are passed to it.

Our network