In this article, we will learn in detail what the difference is between passing parameters by value and by reference in PHP 7.
Value vs Reference
1 - Value. When we perform a parameter pass based on value, it means that a function will perform its operations based on the value of the parameter given to it. In other words, the given value will be altered only within the function's scope. This is the standard way PHP passes parameters.
2 - Reference. When we perform a parameter pass based on reference, it means that a function will perform its operations based on the original reference of the parameter. In other words, if we provide a variable to a function, the value of that variable will be altered in the global scope. To perform this pass, simply add a & (ampersand) before the parameter provided.
What do you think about developing a practical example to better understand this difference?
First Example
We will create a function that will add text to an existing string. We will call this function addNameToString(). Its script will be as follows:
function addNameToString($string) {
$string .= ' - New Name';
return $string;
}
Explaining: this function will expect a string as a parameter. It will add a new text to this string and then return it with its new value. To test if it is working, we will create a variable before it that will contain a name and then provide it as a parameter to the function. The final code will look like this:
<?php
$name = 'Hcode';
function addNameToString($string) {
$string .= ' - New Name';
return $string;
}
echo addNameToString($name);
echo '<br/>';
echo $name;
We created the variable $name, provided it to the function addNameToString() along with the echo constructor, to already display the value returned by the function in the browser. Below this, we broke a line with the
tag and set another echo to see what the original value of the variable is. Let's test this code in the browser. The result will be as follows:
The first line shows our variable with its value altered by the function. However, notice that in the line below, the original value of the variable did not change, it remained the same, as the function altered only the isolated value of the variable $name.
Now let’s develop this same example, but using the reference parameter passing. As we learned above, simply provide the & before the parameter $name in the function addNameToString(). Your code will look like this:
function addNameToString(&amp;$string) {
$string .= ' - New Name';
return $string;
}
Notice that the syntax did not change much. What will be the result of this change in our browser? When we refresh the page, we see the following:
Now both lines return the value altered by the function. That is, the variable $name indeed had its value altered, as the function worked not with an isolated value, but with the reference of the parameter, with the variable $name itself. Amazing, don’t you agree? We can conclude that by passing a parameter by value, it is as if we took a copy of the content of a variable and informed it to the function separately. But by passing a parameter by reference, it is as if we are informing the function the variable in its entirety. We can say that PHP will be able to "understand the reference" ;).
Second Example
Parameter passing can also be widely used in loops. Let's develop an example in this context.
We will create an array with some NBA teams and iterate through its values using a foreach(). The final code will look like this:
<?php
$nba_teams = ['Chicago Bulls', 'Los Angeles Lakers', 'Boston Celtics', 'New York Knicks'];
foreach ($nba_teams as $index => $team) {
$team_number = $index + 1;
$team = "#$team_number - $team";
echo $team;
echo "<br/>";
}
echo '<hr/>';
var_dump($nba_teams);
Explanation: we created the variable $nba_teams, which is an array. Inside the foreach(), which is iterating through this array, we change the value of each item, adding a number in front of it, which will be equal to the index of the array plus one. We will display each new value within the foreach(). At the end of the code, we define a var_dump(), which will display the values of the original array. There will be a
tag among them, which will create a line of separation, making our visualization easier. Let’s run this code in our browser. The result will be as follows:
We notice in the first few lines that the names of the teams have been altered, adding a number before them. However, in the last line, we see that the original value remains. Now let's change the foreach() to the concept of parameters by reference, following the same pattern as the previous example. Our loop will look like this:
foreach ($nba_teams as $index => &amp;$team) {
$team_number = $index + 1;
$team = "#$team_number - $team";
echo $team;
echo "<br/>";
}
The & was added to the variable $team, which represents the name of each team, and which will indeed be altered. The result in the browser will be this:
Note that now both the values in the loop and the original array have been altered, due to the reference we provided to the foreach().
We hope that these two examples have helped you understand a little better the difference between passing parameters by value and by reference. If you liked this content, we kindly ask you to share it with others so that they can also access this tip.
If you wish to learn more about PHP, you can check our Complete PHP 7 Course and become a true specialist in this amazing language.
Until the next article!