PHP Tutorials - Herong's Tutorial Examples - Version 4.20, by Dr. Herong Yang
Variable-Length Argument Lists
This section provides a tutorial example on how to access arguments in a function without using argument variables and how to pass arguments more than what are defined in the function statement.
As I mentioned earlier, PHP also allows you to access arguments provided in the function call operation without using argument variables. Here is how:
1. func_get_arg(position) - It returns the value of the specified argument provided in the function call operation. "position" is the position index of the specified argument. The position index of the first argument is 0. For example, if "func_get_arg(2)" is used in a function, it will return the value of the 3rd argument.
2. func_num_args() - It returns the total number of arguments provided in the function call operation. For example, if "func_num_args()" returns 1, you know that there is only 1 argument provided by calling code.
3. func_get_args() - It creats and returns an array which contains values of all arguments provided in the function call operation. For example, if "$args = func_get_args()" is used in a function, $args will be array containing all arguments.
With all these 3 functions, we say that PHP supports variable-length argument lists with some basic rules:
To help us understand how use variable-length argument list feature, I wrote this tutorial example:
<?php # ArgumentList.php #- Copyright (c) 2003-2019, HerongYang.com, All Rights Reserved. # function f2c($fahrenheit) { $celsius = (func_get_arg(0) - 32.0) / 1.8; return $celsius; } print("\n Calling a function that uses func_get_arg():\n"); if (f2c(20.0)<0.0) print(" It's cold here!\n"); function myMax() { $max = NULL; if (func_num_args()>0) $max = func_get_arg(0); for ($i=1; $i<func_num_args(); $i++) { if ($max < func_get_arg($i)) $max = func_get_arg($i); } return $max; } print("\n Calling a function that uses func_num_args():\n"); print(" ".myMax()."\n"); print(" ".myMax(3)."\n"); print(" ".myMax(3, 1, 4)."\n"); print(" ".myMax(3, 1, 4, 1, 5, 9, 2, 6, 5)."\n"); function myMin() { $list = func_get_args(); $min = NULL; if (count($list)>0) $min = $list[0]; for ($i=1; $i<count($list); $i++) { if ($min > $list[$i]) $min = $list[$i]; } return $min; } print("\n Calling a function that uses func_get_args():\n"); print(" ".myMin()."\n"); print(" ".myMin(5)."\n"); print(" ".myMin(5, 6, 2)."\n"); print(" ".myMin(5, 6, 2, 9, 5, 1, 4, 1, 3)."\n"); ?>
If you run this sample script, you should get:
C:\herong> \php\php ArgumentList.php Calling a function that uses func_get_arg(): It's cold here! Calling a function that uses func_num_args(): 3 4 9 Calling a function that uses func_get_args(): 5 2 1
Last update: 2019.
Table of Contents
Introduction and Installation of PHP 7.3
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
"function" Statements - Defining Functions
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
Introduction of Class and Object
Integrating PHP with Apache Web Server
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