Broad Network


Normal Argument List in PHP

PHP Function Arguments – Part 1

Forward: In this part of the series, we look at the normal argument list of a function.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 1 of my series, PHP Function Arguments. An argument list is just a list of the arguments for a function. You can still call it the parameter list. In this part of the series, we look at the normal argument list of a function.

You need basic knowledge in PHP in order to understand this series. If you do not have that prerequisite knowledge, then read the series I wrote in this blog whose first part is titled, Getting started with PHP. To reach that series, just type the title and my name Chrys in the Search Box of this page and click Search.

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.

Example
In the following function, there are two arguments in the argument list:

<?php

    function myFn($var0, $var1)
        {
            echo $var0."<br />";
            echo $var1."<br />";
        }

    myFn(25, 'man');

?>

Passing Argument by Reference
When you pass a reference to a function, the parameter of the function has to be a reference. The following program illustrates this:

<?php

    function fun(&$var)
        {
            $var = 77;
            echo $var."<br />";
        }

    $var0 = 66;
    fun($var0);
    echo $var0."<br />";

?>

In the above program the parameter is a reference, which is &$var. Now the variable, $var has not been defined anywhere else outside the function. It appears as a function parameter and it is used inside the function. Down in the program, 66 is assigned to the ordinary variable, $var0. The last-but-one statement in the program, calls the function. The argument of the function call is $var0. As soon as the function starts executing, the variable attached to & to form the parameter refers to the same region in memory as the variable sent as argument.

In the above program the function definition modifies the value and echoes it, with the variable, $var. The last statement in the program also echoes the modified value, but this time with the ordinary variable, $var0.

Assume that you have a variable outside a function. If you pass this variable by reference to a function, the function variable and this one will refer to the same value (region) in memory. Any change made to the function variable inside the function, will automatically be the same change made to the variable outside the function.

So to pass a reference to a function, make the parameter a reference with a variable that has not been previously declared; to call the function you send a variable that has been declared outside the function, as argument. In the execution of the function, the two variables will refer to the same region in memory, and the value in the region can be changed with either variable.

An Array as Argument
In the following function, there is only one argument. However, this argument is an array. There are two elements in the array, so you can still say that the argument list has indirectly two elements,

<?php

    $myArr = array(0=>"one", 1=>"two");

    function myFn($arr)
        {
            global $myArr;

            $arr[0] = "aa";
            $arr[1] = "bb";        

            $myArr[1] = $arr[1];
        }

    myFn($myArr);

    echo $myArr[0]."<br />";
    echo $myArr[1]."<br />";

?>

If you pass an array in the ordinary way as in the above code, then you are passing a copy of the array. You end up with two arrays: one outside the function and one inside the function. If you want any changes made to the one inside the function, to appear in the one outside the function, then make a copy of the changes by assignment.

In the above program an array is passed as argument; this means a copy of the array is passed. All the values of the copy array in the function are changed. In order for the change in the second value of the copy array to be made in the second value of the array outside, the last statement in the function actually assigns the second value of the copy array to the second value of the array outside. Read and try the above code.

Passing an Array by Reference
In the following program, an array is passed as reference. This time the function array and the array outside are the same array. That is, the array variable inside the function and the array variable outside the function are referring to the same array (region) in memory. Any change made to the array variable inside the function is the same change made to the array variable outside the function. Read and try this:

<?php

    $myArr = array(0=>"one", 1=>"two");

    function myFn(&$arr)
        {
            $arr[0] = "aa";
            $arr[1] = "bb";        
        }

    myFn($myArr);

    echo $myArr[0]."<br />";
    echo $myArr[1]."<br />";

?>

Default Values
Default values are values that are not sent during the function call. The function should know and use them. Default values are assigned in the parameter list. The following program illustrates this:

<?php

    function myFn($var0, $var1, $var2=10, $var3=4)
        {
            echo "$var0 and $var1 have $var2 books and $var3 pens.";
        }

    myFn("Jahn", "Mary");

?>

In the parameter list, the default value for $var2 is 10 and the default value for $var3 is 4. The first two parameters in the parameter list do not have default values; they are normal parameters and in the function call, only their values are sent as arguments.

Note: In a function, a parameter list can have zero, one or more default values. Know that all default parameters should be on the right of non-default parameters in the parameter list.

Let us end here for this part of the series. We continue in the next part.

Chrys

Related Links

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

Comments

Become the Writer's Fan
Send the Writer a Message