Broad Network


Passing by References in PHP

Understanding PHP Reference Part 3

Forward: In this part of the series, we see how to pass a reference to a PHP function.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 3 of my series, Understanding PHP Reference. In this part of the series, we see how to pass a reference to a PHP function.

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.

Passing a Reference to a Function
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 function, calls the function. The argument to 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.

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.

Returned Reference as Function Parameter
Hey, the function call of a function that returns a normal value can be the argument of a function where there is pass by reference. The following program illustrates this:

<?php

    function fn()
        {
            $varA = 5;
            return $varA;
        }

    function fun(&$var)
        {
            echo $var;
        }

    fun(fn());

?>

The function, fn() returns a normal value. The function, fun() operates through pass by reference; its argument is fn(). When you type this type of argument, do not type a semicolon after the argument inside the parentheses of the calling function. The return value of the argument function is the actual argument of the second calling function.

On the other hand, you can have a function that returns a reference, and then make the function call to that function, the argument of an ordinary function. The following code illustrates this:

<?php

    function &fn()
        {
            $varA = 5;
            return $varA;
        }

    function fun($var)
        {
            echo $var;
        }

    fun(fn());

?>

In the second function call, you call the other function as argument, without the &. The return reference of the argument function is the actual argument of the second calling function.

PHP Object Oriented Programming adds more to the use of references. Search my blog for more info.

We can end here. We continue in the next part of the series.

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