PHP Variables by Reference or Value: A Practical Tutorial

Variables are used to store information like text strings, numbers or arrays. They are the most vital part of the programming. Today, i am going to explain two basic but very, very important concepts of programming about variables.

  1. PHP Variables: Passing by Value
  2. PHP Variables: Passing by Reference

I will explain these important concepts by different examples, So it will help you to understand the core concepts of PHP variables easily.

PHP Variables Passing by Value/Reference: Example 1

This is the simple example which will elaborate the idea of variables passing by value/reference.

<?php
/*
|-------------------
| Variable 1
| Simple Variable Declaration
|-------------------
*/

$variable1 = "Red";
echo "<p>Output 1: Variable 1 is '".$variable1."'</p>";

/*
|-------------------
| Variable 2
| Variable 2 is "Passed by Value" Example
|-------------------
*/

$variable2 = $variable1;
echo "<p>Output 2: Variable 2 is '".$variable2."'</p>";

/*
|-------------------
| Variable 3
| Variable 3 is "Passed by Reference" Example
|-------------------
*/

$variable3 = &$variable2;

/*
|-------------------
| Variable 3
| Let's change variable 3
|-------------------
*/

$variable3 = "Green";

echo "<p>Output 3: Variable 2 is '".$variable2."'<br />";
echo "Output 3: Variable 3 is '".$variable3."'</p>";
?>

Output

Will you guess the Output of Variable 2 at the end of script šŸ˜‰ ? Let’s discuss the tutorial example one by one.

  • Output 1 will be “Red”, as it is simple declaration of variable 1
  • Output 2 will be “Red”, it is an example of “Passed by Value“. So passingĀ  a variable by value means, “Copying a Variable” to a new one, so any future change in the new variable will not affect the original variable.
  • Output 3 will be “Green” for both new and original variables, it is an example of “Passed by Reference“.

Note:

Variable 3 is not a “Copy” of variable 2, but variable 2 has passed by reference to variable 3 by a an operator “& i.e ampersand operator”. So any future change in variable 3 is indeed a change in variable 2. Here is the output of above logic.

Output 1: Variable 1 is 'Red'

Output 2: Variable 2 is 'Red'

Output 3: Variable 2 is 'Green'
Output 3: Variable 3 is 'Green'

PHP Variables Passing by Value/Reference: Example 2

This example will deliver the idea of “Value/Reference” concept in the form of a function.

<?php
/*
|-------------------
| Function
| Variable 1: Passed by Value
| Variable 2: Passed by Reference
|-------------------
*/

function get_change( $variable1, &$variable2 ){
    $variable1 = "Red by Function";
    $variable2 = "Green by Function";
	// Return Nothing
}

/*
|-------------------
| Variable Declaration
|-------------------
*/

$variable1 = "Red";
$variable2 = "Green";

/*
|-------------------
| Function Call
|-------------------
*/

get_change( $variable1, $variable2 );

/*
|-------------------
| Output
|-------------------
*/

echo "<p>Variable 1: " . $variable1 . "</p>";
echo "<p>Variable 2: " . $variable2 . "</p>";

?>

Output

I am sure, you can guess Variable 2 now :). Let’s discuss tutorial example one by one.

  • I have written a PHP method which is elaborating the both concepts of “Variable by Value” and “Variable by Reference”. Variable 1 argument of function is an example of “Passed by Value” whereas variable 2 argument is an example of “Passed by Reference”. So remember our previous example, any change in variable 2 within function will change the original passed variable indeed – as it is not a copy but having address (reference) of passed variable.
  • We have declared two variable 1 and variable 2 with the “Red” and “Green” values respectively.
  • Let’s make a function call.
  • Output of Variable 1 will be “Red” as expected.
  • Output of Variable 2 will be “Green by Function” as our function “get_change” has changed the value of variable 2 due to passed by reference.

Let’s have a look into the output of above logic.

Variable 1: Red

Variable 2: Green by Function

PHP Variables Passing by Value/Reference: Example 3

Example 3 will clarify the concepts of PHP variables passing by value/reference in the form of array and loop.

<?php
/*
|-------------------
| Array 1 Declaration
|-------------------
*/

$array1 = array(
		1	=>	10,
		2	=>	20,
		3	=>	30,
		4	=>	40,
		5	=>	50
	);

/*
|-------------------
| Loop Example 1: Passed by Value
|-------------------
*/

foreach ( $array1 as $val ) {
	$val += 10;
}

/*
|-------------------
| Output Example 1: Passed by Value
|-------------------
*/

echo "<pre>";
print_r( $array1 );
echo "</per>";

/*
|-------------------
| Loop Example 2: Passed by Reference
|-------------------
*/

foreach ( $array1 as &$val ) {
	$val += 10;
}

/*
|-------------------
| Output Example 2: Passed by Reference
|-------------------
*/

echo "<pre>";
print_r( $array1 );
echo "</per>";

?>

Output

I am confident on you now. Yes, you can write the output of both examples before looking into example no 3 output in this tutorial. Let’s dissect the logic,

  • I have declared an Array with the values 10,20,30,40 and 50.
  • “Loop 1” is a simple example of “Variables Passing by Value”. So we will notice that there will be not any change in the array after loop transverse over it. So the the output of array will be 10,20,30,40 an 50 simply.
  • “Loop 2” is not a simple one as we are using “& operator” so it is an example of “Variables Passing by Reference”. So the change within loop is the change to the passed variable (array1) indeed. So the values of array will show an increment of 10 after traversing loop.

Here is the output of our third example


Loop 1 Output

Array
(
    [1] => 10
    [2] => 20
    [3] => 30
    [4] => 40
    [5] => 50
)

Loop 2 Output

Array
(
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
)

I hope you have understood the concept of PHP variables by reference or value in this practical tutorial. Points to remember,

  • In PHP5 variables are by default passed by value and objects are by default passed by reference.
  • Either can be optionally passed by reference using the & operator.

Don’t forget to write your valuable comments and to subscribe yourself to remain up to date with the latest and useful tutorials.

2 thoughts

Comments are closed.