This section describes what is a reference, creating references with & operator, assigning references to variables, removing reference links with unset($var) function.
What is a reference?
A reference is an alias of a variable. PHP supports references in a very similar way as the Perl language.
Here are some basic rules about using references in PHP:
1. To create a reference of a given variable, you need to use the reference operator: &. For example,
&$title creates a reference of variable $title.
2. Reference can be assigned to another variable using the assignment operator: =. For example,
$subject = &$title assigns the reference of variable $title to variable $subject.
3. The variable holding the reference is an alias of the original variable and behaves the same way as
the original variable.
For example, $subject = &$title; var_dump($subject); prints information of the data assigned to variable $title.
4. If the original variable is assigned to a new data, the reference variable is automatically assigned
to that new data.
For example, $subject = &$title; $title = "New String"; assigns "New String" to both $subject and $title.
5. If the reference variable is assigned to a new data, the original variable is automatically assigned
to that new data.
For example, $subject = &$title; $subject = "New Text"; assigns "New Text" to both $subject and $title.
6. Multiple reference variables can be created by assigning the reference to multiple variables.
For example, $subject = &$title; $topic = &$title; assigns the reference of $title to both $subject and $topic.
7. Actually, original variable and its reference variables can all be viewed as references to the assigned data
shared by all of them. For example, $subject = &$title; $topic = &$title; $topic = "New Message"; creates 3 variables
referring to the same data "New Message".
8. To remove the reference link between a variable and its assigned data, you need to use the unset($var) function.
For example; $title = "New String"; unset($title); removes the reference link between $title and "New String".
$title is in the "unset" states now.
9. If multiple variables are referencing the same data, removing the reference link on one variable does not affect
other variables. For example, $subject = &$title; $topic = &$title; $topic = "New Message"; unset($topic);
removes the reference link on $topic, but $subject and $title are still referring to "New Message".
10. If multiple variables are referencing the same data, assigning a new reference to a new data to one variable
does not affect other variables. For example, $subject = &$title; $topic = &$title; $topic = "New Message";
$topic = &$name; assigns the reference of $name to $topic, but $subject and $title are still referring to "New Message".
To show you some of reference rules mentioned above, I wrote the following PHP script, ReferenceTest.php:
<?php # ReferenceTest.php
# Copyright (c) 2005 by Dr. Herong Yang. http://www.herongyang.com/
#
$title;
print "\n \$title is not assigned to anything:\n";
print " \$title: "; var_dump($title);
$subject = &$title;
print "\n \$subject is an alias of $title:\n";
print " \$subject: "; var_dump($subject);
print " \$title: "; var_dump($title);
$subject = "Herong's PHP Book";
print "\n \$subject is assigned with a string:\n";
print " \$title: "; var_dump($title);
print " \$subject: "; var_dump($subject);
$title = "Herong's Programming Book";
print "\n \$title is reassigned with a new string:\n";
print " \$title: "; var_dump($title);
print " \$subject: "; var_dump($subject);
$topic = &$subject;
print "\n \$topic is added as the third reference variable:\n";
print " \$title: "; var_dump($title);
print " \$subject: "; var_dump($subject);
print " \$topic: "; var_dump($topic);
unset($subject);
print "\n \$subject's reference link is removed:\n";
print " \$title: "; var_dump($title);
print " \$subject: "; var_dump($subject);
print " \$topic: "; var_dump($topic);
$name = "Herong's Tutorial Book";
$title = &$name;
print "\n \$title is assigned to a new reference:\n";
print " \$title: "; var_dump($title);
print " \$subject: "; var_dump($subject);
print " \$topic: "; var_dump($topic);
print " \$name: "; var_dump($name);
?>
If you run this sample script, you should get:
$title is not assigned to anything:
$title: NULL
$subject is an alias of :
$subject: NULL
$title: NULL
$subject is assigned with a string:
$title: string(17) "Herong's PHP Book"
$subject: string(17) "Herong's PHP Book"
$title is reassigned with a new string:
$title: string(25) "Herong's Programming Book"
$subject: string(25) "Herong's Programming Book"
$topic is added as the third reference variable:
$title: string(25) "Herong's Programming Book"
$subject: string(25) "Herong's Programming Book"
$topic: string(25) "Herong's Programming Book"
$subject's reference link is removed:
$title: string(25) "Herong's Programming Book"
$subject: NULL
$topic: string(25) "Herong's Programming Book"
$title is assigned to a new reference:
$title: string(22) "Herong's Tutorial Book"
$subject: NULL
$topic: string(25) "Herong's Programming Book"
$name: string(22) "Herong's Tutorial Book"