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 help us understand these rules, I wrote this tutorial example:

<?php # FunctionReturnReferences.php
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
#
   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: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

Function Declaration, Arguments, and Return Values

 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

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Outdated Tutorials

 References

 PDF Printing Version