Passing Arguments to Functions

This section describes rules on how arguments can be passed from the calling code into the called function by value (the default) or by reference.

As shown in examples in previous sections, passing arguments to a function seems to be a simple job. But it may cause confusion if you don't following the rules. PHP has the following rules on passing arguments to function and subroutine procedures:

1. By default, a function argument is passed by value. In this case, the argument variable contains a copy of the data provided by the calling code.

2. But, a function argument can be passed by reference, if you define the argument variable as &$variable_name. In this case, the argument variable contains a reference of the original variable in the calling code. In another word, the argument variable is an alias of the original variable.

3. A pass-by-value argument variable is safer to use, because it will be assigned with a copy of the data from the calling code at the time of call. If the argument variable is re-assigned with a new data in the function, there will be no impact to the calling code.

4. A pass-by-reference argument variable offers an advantage and a risk. The advantage is that it can be used as an output channel for the function to send data back to the calling code. The risk is that if the argument variable is re-assigned with a new data in the function by a mistake, there will be some negative impact on the calling code.

5. An argument variable can be defined with a default value. Arguments with default values must be positioned at the end of the argument list.

6. If you want to use the default value of an argument variable, you can skip that argument in the function call operation.

7. Arguments passed in the function call be retrieved without using argument variables. You can use func_num_args(), func_get_arg(), and func_get_args() functions to access arguments. This feature can be used to support variable-length argument lists.

8. If an array is passed by value, a copy of the original array will be passed to the function. If an array is passed by reference, the function and the calling code is sharing the same array.

9. If a reference of a variable is passed by value, a copy of the reference will be passed to the function. But the copy and the reference are both referring to the same original variable. So passing a reference by value is equivalent to passing a variable by reference.

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