PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Returning References from Functions

This section provides a tutorial example on how to return references from functions - prefix the function name with & in both definition and call statements.

PHP supports function return references using this syntax:

   function &function_name() {
      ...
      return $var_1;
   }
   
   $var_2 = &function_name();

When you use a function to return a reference, you need follow these rules:

  • To declare a function that returns a reference, the function name must be prefixed with '&' in the function definition statement.
  • If a function is declared to return a reference, it should use the 'return' statement to return a variable.
  • To call a function that returns a reference, the function name must be prefixed with '&' in the function call operation.
  • The returning reference from a function should be assigned to another variable.

To help us understand these rules, I wrote this tutorial example:

<?php # FunctionReturnReferences.php
# Copyright (c) 2003 by Dr. Herong Yang. http://www.herongyang.com/
#
   function pvrv($arg) {
      $arg = "Two";
      return $arg;
   }
   print("\n 1. Pass by value and return by value:\n");
   $x = "One"; 
   $y = pvrv($x);
   $y = "Three";
   print("    x: $x\n");
   print("    y: $y\n");

   function prrv(&$arg) {
      $arg = "Two";
      return $arg;
   }
   print("\n 2. Pass by reference and return by value:\n");
   $x = "One"; 
   $y = prrv($x);
   $y = "Three";
   print("    x: $x\n");
   print("    y: $y\n");

   function &pvrr($arg) {
      $arg = "Two";
      return $arg;
   }
   print("\n 3. Pass by value and return by reference:\n");
   $x = "One"; 
   $y = &pvrr($x);
   $y = "Three";
   print("    x: $x\n");
   print("    y: $y\n");

   function &prrr(&$arg) {
      $arg = "Two";
      return $arg;
   }
   print("\n 4. Pass by reference and return by reference:\n");
   $x = "One"; 
   $y = &prrr($x);
   $y = "Three";
   print("    x: $x\n");
   print("    y: $y\n");
?>

If you run this sample script, you should get:

 1. Pass by value and return by value:
    x: One
    y: Three

 2. Pass by reference and return by value:
    x: Two
    y: Three

 3.Pass by value and return by reference:
    x: One
    y: Three

 4. Pass by reference and return by reference:
    x: Three
    y: Three

Test 4. proves that the main code received the reference from prrr() correctly. $y is assigned with the reference of $arg. But $arg is an alias of $x, because $x is passed as a reference. So $y becomes an alias of $x.

Last update: 2005.

Sections in This Chapter

What Is a Function?

"function" Statements - Defining Functions

Function Call Operations

Passing Arguments to Functions

Example of Passing Arguments by Values

Using Pass-by-Value Arguments for References

Example of Passing Arguments by References

Variable-Length Argument Lists

Providing Default Values to Argument Variables

Returning Values from Functions

Returning References from Functions

Dr. Herong Yang, updated in 2009
Returning References from Functions