Today we will learn a very interesting topic called references in this References in PHP tutorial.
- What are references? References are tools in PHP which allows access to a value with the use of different identifiers.
- It means more than one variables point to the same value.
- This means if two variables say $a and $b point the same value and if $a changes its value, $b can access the changed value.
- This is useful when we want to return some value from a function without using the return statement. Since reference changes the value at its original location, it can be accessed anywhere.
- A reference of the value stored in a variable is given to other variable using ampersand (&) sign. Example is given below:
Let us first understand the internal working of references in PHP.
- Internal working of references:
- Let us understand how the storage of references is done and how they are accessed.
- We always need a variable to store a value. In other words variable is the name given to a memory location.
- In PHP all the variables are stored in a container called zval container. It is also called as variable container.
- This container stores all the information related to the value inside variable such as type of value along with the value.
- The symbol table which is analogous to an associative array stores the variables in the form of keys and those keys point to the variable containers that store their values.
- The zval container also stores the reference count which counts the number of references pointing a particular value.
- Let us see an example:
In the above example variable $a has value 8 and it is referenced by variable $b. It will look somewhat like this:
Here, we see that the variable $a has value 8 i.e. it points to the value 8 in zval container. Later a reference of $a is given to $b. I mean if a value of variable is assigned to another variable using ampersand (&) sign, it is said to be assigned by reference. So the variable $b also points to the zval container to which the variable $a points instead of creating a new copy of value.
We can see a flag is_ref=1; this flag initially has value 0 (zero) but when we assign the value by reference to any variable, this flag becomes 1 and if the value is not assigned by reference, it remains zero.
The reference counter refcount counts the number of references pointing to the variable container containing the value.
Now if we allow another variable $c to point to value 8 by assigning the reference of value in $a i.e. $c=&$a; it will also point to the same zval container as $a and $b and the value of refcount will increase by 1 and become 3 as shown below:
Now if we assign another value to variable $c like $c=56; the variable $c will now point to the zval container containing value 56 and the pointer pointing value 8 will be removed as shown in the figure below:
Here you can see that the variable $c now points only to value 56 and its reference count is 1. And $c variables pointer to value 8 is removed and with it the refcount is decreased by 1 because one reference has been removed.
In this way the zval container keeps track of all the information related to the value stored in it.
Operations using references:
- Now let us learn how to use references in our programs.
- There are three basic operations performed by references:
Assigning by reference:
- Assigning by reference
- Passing by reference
- Returning by reference
- Assigning by reference means allowing two variables to point to the same content or value. We have seen it above but still see the example below:
echo "val1 = ".$val1."<br>";
echo "val2 = ".$val2;
Here we have a variable $val1 which is assigned a value 100. The second variable $val2 is assigned a reference of value 100. This is done using the ampersand (&) sign.
When we assign any variable value to another variable with the help of ampersand (&) symbol, we actually assign the reference of it to another variable. In the above case we have assigned the reference of $val1 to $val2. It means that we have allowed $val2 to point to the value to which $val1 is pointing.
The output of the above code is shown below:
The variables $val1 and $val2 don’t point to each other, but they both point to the container containing the same value. And so if any one variable changes the value, the other can also access it .Please see the continuation of above program below:
echo "<br>val1 = ".$val1;
Here, we have assigned a value 50 to variable $val2. But we know that the variable $val2 is pointing to the variable container. This means if $val2 is assigned value 50, the value 100 in the container is replaced by 50. We also know that the same container is also pointed by variable $val1, so $val1 also has access to it.
So when $val2 is assigned value 50, it is stored in the variable container and the variable $val1 accesses it in the above code. The output is shown below:
In the above figure you can see that the original value pointed by variables $val1 and $val2 was 100. And when the value 50 is assigned to variable $val2, it is accessed by $val1 because the same value is accessible to both of them.
Passing by reference:
- We learned assigning a value by reference, but we can even pass values to a function by reference.
- We know that we can pass values to functions by simply passing a simple variable in a function call and accepting it in a simple normal variable in the function definition. This is about the simple call by value method of functions.
- But in call by reference the variable in function call is passed similar to call by value but in the function definition an ampersand sign is used with the parameter which is to be passed by reference.
- Let us see an example:
echo "Cube of 10 = $n.";
Here, the output will be Cube of 10 = 1000. We have passed the value in variable $n by reference to the function cube(). Since it is passed by reference, the variables $n and $no both point to the variable container holding value 10.
In the function the cube of value 10 is calculated and again assigned to variable $no.
Since both the variables point to same location, the same location is changed and can be accessed by both the variables.
Using such pass by reference functions there is no need to even return a value. And you can modify more than one location values at a time using pass by reference.
You can pass a variable, a new statement or a reference returned from a function into a function called by reference.
Returning a reference:
- Even though we can modify more than one values by pass by reference method, there are some situations where returning a value is necessary using return statement.
- To return a value by reference, the function that returns the value is preceded by an ampersand (&) symbol.
- Let us see a simple example:
public function __construct($n)
public function &SQ()
$s=$this->no * $this->no;
echo "Square of 10 = $s";
In the above code we have declared a class called square. It has a function SQ() which is going to return a value by reference. That is why it is preceded by an ampersand (&) symbol.
$sq is the object of class square and we have sent value 10 in its constructor. Then we called the function SQ() using object $sq and stored the returned value in variable $s which is then displayed using echo statement.
The output is shown below:
Unsetting a reference:
- Unsetting a reference means, no more allowing a variable to point to the variable container containing a value.
- unset() method is used to unset a reference variable pointing the value.
- Once the reference is unset, it stops pointing the container containing the value and then is ready to be garbage collected.
- Example is as follows:
- Suppose variables $a, $b and $c point to the zval container containing value 8 as shown below:
Now if we unset reference $c. The code is given below:
The variable $c will stop pointing value 8 as shown below:
This variable $c is now ready to be garbage collected.
- PHP allows you to use a global variable inside a function.
- The global keyword allows you to do so.
- The global keyword creates a reference between a global variable and the local variable of a function.
- The global variable will be similar to $var=& GLOBALS[“$val”];
- Let us try an example:
Here, we assigned a string Caroline to variable $val. It is a variable outside the function.
A function changeval() is written to change the value of $val variable.
We have accessed the variable $val inside the function by using the global keyword.
After changing the value by the value passed in the function, it is returned back using the return statement.
We can see that the outside variable $val is effectively used inside the function with the use of global keyword.
Value of $val before changing and after changing is displayed as follows:
Thus we studied the references in PHP in this References in PHP tutorial. But references should be used when they are really needed, they can create complications and errors in some places which cannot be solved easily. So use them if important.