Broad Network


Reference in PHP

PHP References - Part 1

Foreword: In this part of the series, I talk about the basics of reference in PHP.

By: Chrysanthus Date Published: 2 Dec 2018

Introduction

This is part 1 of my series, PHP Reference. In this part of the series, I talk about the basics of reference in PHP. You should have read the previous series before reaching here, as this is the continuation.

Memory Region
A region in memory is a set of consecutive memory cells. Values (variable contents) are kept in memory regions. A variable identifies a memory region. A reference is a pointer to a memory region and is generally used when you are interested in what is in the memory region and not the variable itself. Consider a reference as the address of a memory region. This memory region can have a value.

A memory region is an area in computer memory, that holds the value of a variable.

Same Value with Different Variables
Consider the following two consecutive statements:

  $myVar = "I am the content of a large text file from the hard disk, now in memory.";
  $aVar  = "I am the content of a large text file from the hard disk, now in memory.";

You have two different variables with different names but with the same string value. A variable identifies a memory region. Two different variables with two different names identify two different memory regions, everything being equal. In the above case, the two values, though are the same, are in two different memory regions.

Same Memory Region for two Different Variables
In PHP you can make the same memory region have two different variables. The two different variables will of course, identify the same value. Consider the following consecutive two statements:

  $myVar = "I am the content of a large text file from the hard disk, now in memory.";
  $hisVar  = &$myVar;

For the first statement you have a value assign to the variable, $myVar. In the second statement, $myVar is preceded with the & sign before being assigned to a new variable, $hisVar. & is like an operator. In the second statement, the operator, preceding the first variable, makes the second variable identify the same memory region (same value) as the first variable. An important thing to note here is that $myVar refers to a memory region.

For the second statement, $myVar, with &, is a reference (address of the memory region identified by $myVar). Well, in PHP you can use either $myVar or $hisVar to access the value (same content in same memory region).

Using a Reference
Now that you have a reference, how can you get the value of the memory region that the reference variable is referring to? That is simple in PHP: just use the new variable in the same way that you use the original variable. Try the following code:

<?php

    $myVar = "I am the content of a large text file from the hard disk, now in memory.";
    $hisVar  = &$myVar;

    echo $myVar;
    echo "<br>";
    echo $hisVar;

?>

The output is:

    I am the content of a large text file from the hard disk, now in memory.
    I am the content of a large text file from the hard disk, now in memory.

In PHP a reference variable (e.g. $hisVar) is like a synonym to the original variable.

Reference and Array
In PHP a scalar is an int, a bool, a float or a string. The above discussion works for all scalars. What about the array?

Reference works in the same way with the array as it works with the string. Try the following code:

<?php

    $arr = array('John', 'Peter', 'Mary', 'Susan', 'Alice');

    $arrRef = &$arr;

    for ($i=0; $i<count($arrRef); ++$i)
        {
            echo $arrRef[$i], ' ';
        }

?>

The output is:

    John Peter Mary Susan Alice

Reference and Object
Reference works in the same way with the object as it works with the string. Try the following code:

<?php

    class Cla
        {
            public $prop = 'We are the world.';

            public function mthd()
                {
                    echo $this->prop, '<br>';
                }
        }

    $obj = new Cla();
    $objRef = &$obj;
    $obj->mthd();
    $objRef->mthd();

?>

The output is:

    We are the world.
    We are the world.

Why use Reference
Imagine that you copy the content of a large file from the hard disk to the memory. You may want to use different variables to refer to this content. If the variables you use are not references, then you would have many copies of the same content in memory. That will not be an economical use of memory. Memory to the computer is similar to money to people; memory is always scarce. I hope you see why references are useful.

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

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

NEXT

Comments