Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The return statement is a language construct that immediately terminates the execution of the current function, method, or script, and passes control—along with an optional evaluated value—back to the calling environment.

Syntax

return;
// or
return expression;
Because return is a language construct and not a function, parentheses around the evaluated expression are not required. Their use is generally discouraged in PHP, as it unnecessarily evaluates the expression within parentheses and misrepresents the construct as a function call.
return $value;   // Preferred
return($value);  // Discouraged

Execution Mechanics

  • Termination: When the PHP parser encounters a return statement, it halts execution of the current scope block. Any code physically located after the return statement within that same execution path is unreachable.
  • Evaluation: The expression is evaluated before being passed back to the caller.
  • Implicit Returns: If the return statement is omitted from a function, or if it is called without an expression (i.e., return;), PHP implicitly returns null.
  • Try/Catch/Finally Execution: If a return statement is encountered inside a try or catch block, the finally block will still execute before control and the evaluated value are actually passed back to the caller.

Scope Contexts

The behavior of return changes depending on the execution context:
  1. Function/Method Scope: Terminates the function and yields the evaluated expression to the caller.
  2. Global Scope: Terminates the execution of the main script.
  3. Included/Required Files: If executed within a file loaded via include or require, execution of that specific file halts. Control returns to the parent script, and the return value becomes the evaluated result of the include/require call itself.

Return Type Declarations

PHP allows strict enforcement of the returned value’s data type via function signatures. If a return type is declared, the evaluated expression must match this type (or be coercible to it, depending on strict_types configuration).
function getInteger(): int {
    return 42; // The expression must evaluate to an integer
}

function getVoid(): void {
    return; // Must not return a value; omitting 'return' is also valid
}

Returning by Reference

By default, PHP returns values by value (creating a copy). To return a variable by reference—allowing the caller to modify the original variable’s memory address—the function declaration must be prefixed with an ampersand (&), and the assignment at the call site must use the reference operator (=&). The return statement itself does not use an ampersand.
function &returnByReference(&$variable) {
    // The return statement uses standard syntax; no ampersand is used here
    return $variable; 
}

$myVar = 10;
// The ampersand is required at the call site during assignment
$ref =& returnByReference($myVar);

Compound Returns

PHP does not natively support returning multiple discrete variables. The mechanical workaround is to return a single compound data structure (such as an array or an object) and utilize symmetric array destructuring at the call site.
function returnCompound(): array {
    return ['first_value', 'second_value'];
}

// Destructuring the returned array into separate variables
[$a, $b] = returnCompound();
Master PHP with Deep Grasping Methodology!Learn More