Broad Network


Variable Length Argument List

PHP Function Arguments – Part 2

Forward: In this part of the series we look at variable length argument list.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 2 of my series, PHP Function Arguments. In this part of the series we look at variable length argument list.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

The $… Parameter
The $… parameter means, “and so on”. If you do not know the number and different parameters that your function will take, use the $… parameter in the parameter list. It is an example of what is known as a pseudo type. It is normally used in function syntax and not in code.

Indefinite Argument List
In PHP, when typing the parameter list of a function, you do not have to type all the parameters that the function would need when the function is called. The following function definition and function call is accepted:

<?php

    function myFn($var0, $var1)
        {
            //statements
        }

    myFn(10, 20, 30);

?>

In the parameter list, there are two parameters, but in the function call there are three arguments. That situation is accepted. There are some predefined functions you can use to know inside the function definition, how many arguments were sent in the function call and which arguments were sent in the function call. We look at those predefined functions for the rest of this tutorial.

Note: with the predefined functions, you can actually have a user function definition without any parameter, but the function call, will have as many arguments as you want.

The func_num_args() Function
There is the predefined function called, func_num_args(). This function is used inside the definition of a user function to know the number of arguments that the function call sent. The syntax of the function is:

        int func_num_args ( void )

It returns the number of arguments sent in the function call. The following program illustrates this:

<?php

    function myFn($var0, $var1)
        {
            $numArgs = func_num_args();
            echo $numArgs;
            //function statements
        }

    myFn(10, 20, 30, 40);

?>

The func_get_arg() Function
This function, used inside the definition of a user function returns the value of any argument that was sent in the function call, as follows: The argument list is the list of arguments sent in the function call. The values in this list are numbered beginning from zero (from the left of the arguments sent). The function uses these numbers to get the corresponding argument. The syntax of the function is:

        mixed func_get_arg ( int $arg_num )

In this syntax, the returned mixed type means any data type. There is one argument in the predefined function; it is the position number of the argument value in the argument list sent. Read and try the following code:

<?php

    function myFn($var0, $var1)
        {
            echo func_get_arg (0)."<br />";
            echo func_get_arg (1)."<br />";
            echo func_get_arg (2)."<br />";
            echo func_get_arg (3)."<br />";
            //function statements
        }

    myFn(10, 20, 30, 40);

?>

The func_get_args() Function
This predefined function behaves like the one above, but it returns an array of the argument list and not individual values. You can then get the individual values from the array. The syntax of the function is:

        array func_get_args ( void )

The following program illustrates the use of the predefined function (read and try it):

<?php

    function myFn($var0, $var1)
        {
            $arr = func_get_args ();
            echo $arr[0]."<br />";
            echo $arr[1]."<br />";
            echo $arr[2]."<br />";
            echo $arr[3]."<br />";
            //function statements
        }

    myFn(10, 20, 30, 40);

?>

Any of these three predefined functions is used inside the user function definition. You can use one, two or all three predefined functions in one user function.

For the last two functions, you need to know which value you typed in which position in the argument list sent, in order to know what to do with it.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

Comments

Become the Writer's Fan
Send the Writer a Message