Broad Network


Returning References in PHP

Understanding PHP Reference – Part 2

Forward: In this part of the series, we see how a PHP function can return by reference.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 2 of my series, Understanding PHP Reference. In this part of the series, we see how a PHP function can return by reference.

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.

Return value of a Function
A function can return a value, such as 15, or a string literal. A function can also return a reference, for example something like &$var. For a function that returns a reference, it is like you have to put the function in-between & and $var.

Function Returning a Reference
Consider the following code:

<?php

    function &func()
        {
            $myVar = 25;
            return $myVar;
        }

    $var = func();

    echo $var;

?>

You have the definition of the function, func. In the function description, the name of the function begins with &. This means the function will return a reference and not the value. In the function definition, you return the value ($myVar above). Because of the preceding & in the function name, the reference to the region in memory that keeps the returned value is what is actually returned. This returned reference is assigned to an ordinary value in a function call statement. Try the above program if you have not done so.

So to return a reference, precede the function name in the function description with &. When the function returns a value, a reference to that value is actually returned. Read and try the following code whose function definition does not have a variable:

<?php

    function &func()
        {
            return 15;
        }

    $var = func();

    echo $var;

?>

In this case, there is no original variable holding the value of interest (15). However the reference to the value stored somewhere in a region in memory is actually returned.

Note: When calling the function that returns a reference, you do not precede the function call with &.

Confirming Returning by Reference
In the following program, you have two global variables. You also have a function and a call to the function. The function modifies the value of the first global variable. The function call returns the reference to the global variable, after it has been modified inside the function. This returned reference is assigned to the second global variable. The two variables are then echoed displaying the same changed value, confirming that a reference has been returned. The returned reference is the reference to the value of the first global variable.

<?php

    $var1 = 25;

    function &func()
        {
            global $var1;
            $var1 = 37;
            return $var1;
        }

    $var2 = func();

    echo $var1."<br />";
    echo $var2."<br />";

?>

We have learned how to return by reference. In the next part of the series, we shall learn how to pass by reference.

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