Basics of PHP Reference
Basics of PHP with Security Considerations – Part 13
Foreword: In this part of the series, I talk about PHP Reference Basics.
By: Chrysanthus Date Published: 18 Jan 2018
Introduction
Memory Region
A memory region is an area in computer memory that holds the value of a variable.
Different Memory Regions 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 values. 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, even 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).
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.
Reference and 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
In PHP a reference variable is like a synonym to the original variable.
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.
Let us take a break here and continue in the next part of the series.
Chrys
Related Links
Basics of PHP with Security ConsiderationsMore Related Links
Pure PHP Mailsend - sendmail
PurePHP MySQL API
Using the PurePHP MySQL API
Cousins
JavaScript/ECMAScript Course
Perl Course
BACK NEXT