Example of Passing Arguments by Values

This section provides a tutorial example on how to use pass-by-value arguments, which allows the function to re-assign new values to original variables in the calling code.

As we learned from the previous section, PHP functions use pass-by-value arguments by default. If an argument is defined as an pass-by-value argument, the calling operation can provide an expression for this argument. That expression will be assigned to this argument variable when function is called. For example, if we have "function f($var) {...}" defined, "f(exp)" is equivalent to "{$var=exp; ...}".

To see how passing arguments by values works, I wrote this tutorial example, PassByValue.php:

<?php # PassByValue.php
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
# 
function swap($left, $right) {
   $temp = $left;
   $left = $right; 
   $right = $temp;
   print("    Swapped in function: ".getString($left, $right)."\n");
}

   date_default_timezone_set("Europe/Paris");

   print("\n 1. Passing two literals:\n");
   print("    Before call: ". getString("Apple", "Orange") ."\n");
   swap("Apple", "Orange");

   print("\n 2. Passing two variables:\n");
   $x = "Dog"; $y = "Cat";
   print("    Before call: ". getString($x, $y) ."\n");
   swap($x, $y);
   print("    After call: ". getString($x, $y) ."\n");

   print("\n 3. Passing two arrays:\n");
   $x = array("Mon", "Tue"); $y = array("Jan", "Feb");
   print("    Before call: ". getString($x, $y) ."\n");
   swap($x, $y);
   print("    After call: ". getString($x, $y) ."\n");

   print("\n 4. Passing two objects:\n");
   $x = new DateTime("2005-01-01"); $y = new DateTime("2006-01-01");
   print("    Before call: ". getString($x, $y) ."\n");
   swap($x, $y);
   print("    After call: ". getString($x, $y) ."\n");

function getString($left, $right) {
   if (is_scalar($left)) {
      return "$left | $right";
   } elseif (is_array($left)) {
      return "($left[0], $left[1]...) | ($right[0], $right[1]...)";
   } elseif (is_object($left) && get_class($left)=="DateTime") {
      return $left->format("Y")." | ".$right->format("Y");
   } else {
      return NULL;
   }
}
?>

If you run this sample script, you should get:

 1. Passing two literals:
    Before call: Apple | Orange
    Swapped in function: Orange | Apple

 2. Passing two variables:
    Before call: Dog | Cat
    Swapped in function: Cat | Dog
    After call: Dog | Cat

 3. Passing two arrays:
    Before call: (Mon, Tue...) | (Jan, Feb...)
    Swapped in function: (Jan, Feb...) | (Mon, Tue...)
    After call: (Mon, Tue...) | (Jan, Feb...)

 4. Passing two objects:
    Before call: 2005 | 2006
    Swapped in function: 2006 | 2005
    After call: 2005 | 2006

The output confirms that:

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