Broad Network


Variables as Synonyms in PHP

Understanding PHP Reference – Part 1

Forward: In this part of the series, we look at the basics of PHP Reference.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 1 of my series, Understanding PHP Reference. In this part of the series, we look at the basics of PHP Reference. Everything discussed here is applicable to PHP 5.

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.

Region in Memory
A region in memory is a consecutive set of cells in the computer’s memory. A memory region can hold a datum, e.g. an integer or a floating-point number or string. A region can also hold an instantiated object. Consider the following statement:

        $myVar = 56;

In this statement 56 is an integer, which is in a memory region. At the moment this 56 is identified by the variable, $myVar.

Make another Variable Identify the Same Region
You can make another variable identify the memory region already identified for 56 in the above statement. The following code segment does this:

        $myVar = 56;
        $herVar = &$myVar;

There are two statements here. The first one initializes a variable in the normal way, assigning 56 to $myVar. The second statement also initializes a new variable. The right operand of this second statement is the previous variable preceded by the ampersand, &. The $myVar variable already identifies the region in memory that has 56. By preceding it with & in the second statement and assigning the result to the new variable, $herVar, you are making the new variable identify the same memory location that has 56. Now, $myVar and $herVar identify the same region in memory that has, 56. At this point, you can obtain 56 using either $myVar or $herVar.

What is a Reference in PHP?
This is what the PHP specification says about reference in PHP: “PHP references allow you to make two variables to refer to the same content”. In this quotation, “content” means, the value in the region. In the above code segment repeated below, 56 is the value in a region.

        $myVar = 56;
        $herVar = &$myVar;

In this code segment, we can say, &$myVar is a reference. We can say that if you precede a variable with &, you get a reference.

Dereferencing
Dereferencing means obtaining the value from a reference. In the above code segment, &$myVar cannot return the value. Dereferencing a value in PHP is easy: just use (type) the original variable or the variable that received the reference. For the above code segment, $myVar or $herVar will produce the value. Try the following code:

<?php

        $myVar = 56;
        $herVar = &$myVar;

        echo $myVar."<br />";
        echo $herVar."<br />";

?>

Many Variables Referring to One Value
In the above program, two variables (the original and the new variable) are referring to the same value. You can make more than two variables refer to the same value, which is in a particular region in memory.  In the following program that works, reference from the same memory region (having a value) is assigned to three variables, giving a total of 4 variables referring to the same region in memory; the reference is developed from the original variable before being assigned to the other three.

<?php

    $var0 = "PHP is good.";
    $var1 = &$var0;
    $var2 = &$var0;
    $var3 = &$var0;

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

?>

In the following program, a reference is developed from the original variable and then assigned to a second variable; another one is developed from the second variable and then assigned to a third variable; and another one is developed from the third variable and then assigned to the fourth variable:

<?php

    $var0 = "PHP is good.";
    $var1 = &$var0;
    $var2 = &$var1;
    $var3 = &$var2;

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

?>

So, to assign a reference to a variable, you can use the original variable or a variable that was previously assigned a reference.

Advantage of Using a Reference
When more than one variable refer to the same region as opposed to referring to different regions having the same content, you economize memory. However, there would be times when you would want two or more different variables to refer to two or more regions having the same content, because you intend to change the content of the variables. In that case do not use reference.

Assignment Without &
Consider the following code segment:

    $variabA = 6;
    $variabB = $variabA;

From the first statement, $variabA identifies a memory region that has the value, 6. Because, & is not used in front of $variabA in the second statement, $variabB identifies a different region in memory having the value, 6, copied from the region identified by $variabA. After the second statement, there are two regions in memory having the value 6: one identified by $variabA and the other identified by $variabB. If this is a waste of memory, then use reference.

We have seen the basics of reference in PHP. Let us take a break and 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