PHP Returning Values by Reference  screenshot

PHP Returning Values by Reference

PHP Returning Values by Reference

PHP has more than 700 in-built functions. However, sometimes, it is necessary to make user-defined functions to increase re-using PHP code within a fully functioning application. A functions runs when called within PHP scripts. In other words, you must trigger the use of a function by referencing to it. Functions make your work easier and allow you to make simple codes that are easier to maintain. In PHP, you can get the results of a function by value or by reference. In this guide, I will show you how to do the latter since the former is the default behavior of all PHP functions.

Let’s create a single function and see its behavior. Remember, we can supply arguments or variable inputs to a function. Most of the user-defined functions require arguments being passed to them. In the first example here, I will demonstrate how to get the results of a PHP function by value (the default behavior). I will then proceed to return by reference so that you compare both codes and functionalities.

Returning by value

<?php
function samplefunction($parameter1) {
    $parameter1 = "PHP is good - " . $parameter1;
    return $parameter1;
}
$parameter1 = "This is a sample text...";
$result = samplefunction($parameter1);
echo $result;
?>

In the above example, we have a function called “samplefunction” which requires a single input parameter “$parameter1”. The result displayed will only show what our functions returns - that is “PHP is good sample text...”

Returning by reference

On the other hand, PHP allows returning references from a function. However, to declare such a function, you must add an ampersand sign “&” just before the function name. In addition to this, all functions that return values by reference must use the return statement. Calls to the functions that return value by reference must also include the “&” sign. To avoid confusion and interference of the inner working of php, returned references should be assigned to a different variable.

Here is some illustration:

<?php
function &samplefunction($parameter1) // note we have used an & symbol here
{
    $parameter1 = "PHP is good " . $parameter1;
    return $parameter1; // return statement is a must for referenced functions
    
}
$parameter1 = "sample text..."; // this is the parameter passed to the function below
$result = & samplefunction($parameter1); //returned value is assigned to a different variable and an & symbol is included here
echo $result; // we echo out the output
 
?>

To avoid errors while dealing with reference from a PHP function, just remember to put an “&” symbol in front of the function name. Then, remember to do the same when you call the function. This way, you can use the returned parameter as a variable as opposed to a function or value.

In all cases, PHP allows you to reference the returned value. According to PHP industry best programming methods, references should be used sparingly e.g. while returning arrays that can need modification within the code. However, use of referenced functions is great because it can save time and help you create more understandable code that can be reused in different procedures.

Remember, there are many different scenarios where PHP references are great especially when dealing with arrays as objects as opposed to values.