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: 31 Aug 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).
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.
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.
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.
Let us take a break here and continue in the next part of the series.
Chrys
Related Links
Basics of PHP with Security ConsiderationsWhite 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