Example of Passing Arguments by References

This section describes a second possible PWS Trojan infection that executed my.exe from C:\WINDOWS\Temp and another .exe file from C:\WINDOWS\system32. Both of them tried to install .sys files.

As we learned from the previous section, PHP supports pass-by-reference arguments if they are defined with a & prefix on the argument variables. If an argument is defined as an pass-by-reference argument, the calling operation can provide a variable, for this argument. The reference of that variable will be taken and assigned to this argument variable when function is called. For example, if we have "function f(&$var) {...}" defined, "f($arg)" is equivalent to "{$var=&$arg; ...}".

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

<?php # PassByReference.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:

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

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

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

The output confirms that:

Exercise: Try to test pass-by-reference arguments with:

   swap("Apple", "Orange");
   swap(array("Mon", "Tue"), array("Jan", "Feb"));
   swap(new DateTime("2015-01-01"), new DateTime("2016-01-01"));
   swap(&$a, &$b);

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