References and Variables

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) HerongYang.com. All Rights Reserved.
#
   $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 in PHP 7 and higher, you should get output with some "Notice" messages. This is because now the default message level is set to E_NOTICE. You can change the setting or modify the code to make sure all variables are defined before using them.

C:\herong> \php-7.3\php ReferenceTest.php

 $title is not assigned to anything:
    $title: PHP Notice:  Undefined variable: title in
       C:\herong\ReferenceTest.php on line 7

Notice: Undefined variable: title in
   C:\herong\ReferenceTest.php on line 7
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: PHP Notice:  Undefined variable: subject in
       C:\herong\ReferenceTest.php on line 33

Notice: Undefined variable: subject in
   C:\herong\ReferenceTest.php on line 33
NULL
    $topic: string(25) "Herong's Programming Book"

 $title is assigned to a new reference:
    $title: string(22) "Herong's Tutorial Book"
    $subject: PHP Notice:  Undefined variable: subject in
       C:\herong\ReferenceTest.php on line 41

Notice: Undefined variable: subject in
   C:\herong\ReferenceTest.php on line 41
NULL
    $topic: string(25) "Herong's Programming Book"
    $name: string(22) "Herong's Tutorial Book"

If you run this sample script in PHP 5 and lower, you should get output without any "Notice" message:

C:\herong> \php-5.6\php ReferenceTest.php

 $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"

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.3

 PHP Script File Syntax

 PHP Data Types and Data Literals

Variables, References, and Constants

 Variables and Assignment Operations

References and Variables

 Variable Variable Name - Name Variables with Expressions

 Constant and define() Function

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 Managing File Upload

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Parsing and Managing HTML Documents

 Configuring and Sending out Emails

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Outdated Tutorials

 References

 Full Version in PDF/EPUB