Multiple returns from a function

Multiple returns from a function

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

Stefan Bogdanescu

Founder & Senior Architect

Multiple Returns from a Function: Why It Doesn't Work and What to Use Instead

You cannot execute two return statements within a single function invocation in PHP, regardless of the language version you are running. When you write code like the example below, only the first line actually executes before the function terminates immediately.

function test($testvar)
{
  // Do something

  return $var1;
  return $var2;
}

The second return statement is effectively dead code. Once the interpreter processes the first return, it discards the rest of the function block, meaning $var2 will never be evaluated or sent to the caller in that specific path. If your goal is to conditionally return different values based on logic inside the function, you must ensure only one return statement exists per execution path.

Conditional Logic as the Standard Pattern

The idiomatic way to handle multiple potential exit points is to use conditional structures like if, elseif, and else. This approach allows you to control flow without creating unreachable code.

function test($testvar)
{
    if ($testvar === 'a') {
        return $var1;
    } elseif ($testvar === 'b') {
        return $var2;
    } else {
        // Handle default case or throw an exception
        return null; 
    }
}

This pattern is robust and readable. It clearly delineates the specific conditions under which each value is returned. If you find yourself nesting multiple if statements deep within a function, it often signals that your logic could benefit from being split into smaller, single-purpose functions.

Returning Collections for Multiple Values

If your requirement is genuinely to extract multiple distinct results from a function call—such as retrieving both an error message and a data object simultaneously—you should return a composite structure rather than trying to chain returns. The most common patterns in PHP are arrays, objects, or custom value objects.

Returning an array is the simplest approach for lightweight data:

function getUserAndStatus($id)
{
    // Logic to fetch user
    $user = ['name' => 'Alice'];
    
    // Logic to fetch status
    $status = 'active';

    return [$user, $status];
}

// Consumption
list($userData, $userState) = getUserAndStatus(1);

For more complex data contracts, returning an object is often preferred because it allows for named properties and better type hinting support in modern PHP versions. This aligns well with the principles of immutability and explicit interfaces.

The Edge Case: yield and Generators

There is one specific scenario where a function can technically "return" multiple times, but this involves generators rather than standard functions. By using the yield keyword instead of return, you create an iterator that produces values one by one over time. This is essential for processing large datasets without loading everything into memory at once.

However, this changes the contract of your function entirely. A generator returns an object implementing Iterator or Traversable, not a single value immediately. You must iterate over the result using a loop or the foreach construct. Mixing standard return statements with yield in the same function is strictly forbidden and will cause a fatal error.

Trade-offs and Best Practices

The primary trade-off here is between flexibility and clarity. While returning an array offers maximum flexibility, it couples your API to a specific data structure that might evolve. Returning typed objects provides better documentation via IDE autocomplete and stricter type checking but requires more boilerplate setup.

Attempting to force multiple return statements in a synchronous context is almost always a code smell indicating that the function's logic is too complex for a single scope. Refactoring such functions into smaller units usually results in cleaner, more maintainable codebases. For teams working on large-scale applications, adhering to these patterns prevents subtle bugs where expected data simply doesn't arrive because the function exited early.

If you are building enterprise-grade applications and need rigorous contract enforcement for multiple return values, consider leveraging type hints with union types or custom classes that define the output schema explicitly. This ensures that any changes to the function's internal logic do not silently break downstream consumers who expect specific data shapes.

Our network