Functions can return values: there I said it. When we looked at functions previously, those functions just echoed some string. Although that is one use of a function, functions are more powerful than simply echoing out statements. We can use functions to do something and then return a calculated value to us. That value can then be echoed out or assigned to a variable.
To return a value, you will use the return keyword inside of your function. Make sure that the return statement is the last statement inside of your function since PHP will exit the function after the return statement is executed.
Let’s look at a basic example of a function returning a value. This function will return the value 1 when it’s called. That value will then be assigned to the variable $num.
<?php
function basic() {
return 1;
}
$num = basic();
?>
Pretty simple so far. Let’s look at another example. This time, we’ll perform some calculation on an argument that’s passed to the function. The function will then return the calculated result.
<?php
function do_secret_calculation( $secret_num_one, $secret_num_two ) {
$x = $secret_num_one * 19;
$y = $secret_num_two * 34;
$z = $x + $secret_num_one - $secret_num_two + $y;
return $z;
}
$num1 = do_secret_calculation(10, 33);
$num2 = do_secret_calculation(22, 12);
var_dump($num1);
var_dump($num2);
?>
Let’s walk through the example.
- PHP starts the execution process on line 13 by calling the do_secret_calculation(10, 33) function and passing it two arguments, 10 and 33.
- PHP enters the do_secret_calculation() function and starts executing the code.
- Two local variables are created, $x and $y. The first variable, $x, takes the first argument and multiplies it by 19. The second variable, $y, takes the second argument and multiplies it by 34.
- A third local variable, $z, is created. The value is calculated and is assigned to $z.
- The return statement is called. It returns the value that’s stored in the local variable $z.
- PHP exits the function and returns to line 13 with the returned value. That returned value is stored in $num1.
- PHP moves to line 14 and repeats the same process with two new arguments.
Let’s revisit a statement that I mentioned earlier: PHP exits the function as soon as it’s done with the return statement.
<?php
function assign_one() {
$x = 1;
return $x;
$x++;
}
$one = assign_one();
var_dump($one);
?>
What’s going on in this code?
- The function assign_one() is called on line 11.
- PHP enters the function assign_one() and assigns the value 1 to a local variable $x.
- It then moves to the next statement and returns the value stored in $x.
- PHP exits the function and returns to line 11. It does not execute $x++ on line 8.
- The value 1 is assigned to the variable $one.
As you can see, PHP never reaches the statement after the return statement.
Functions can return any data type: they’re not required to return just integer values. You also don’t have to assign the function call to a variable or use a local variable inside of the function like we’ve been doing so far (I did it just to be explicit).
In the example below, the function display_two() is returning the string two. We can concatenate that function call inside of the echo statement. This way, PHP sees the echo statement and starts its execution. It tries to merge the two values together since it encountered the concatenation operator. Before it can do that, it first has to call the function display_two(), which returns the value two. The two strings are concatenated and the full string is echoed out.
<?php
function display_two() {
return "two";
}
echo "You are returning the number " . display_two();
?>
Let’s look at returning one additional data-type: the array.
<?php
function i_am_an_array() {
return [
"one",
"two",
"three"
];
}
var_dump( i_am_an_array() );
?>
This time, the array was returned without being assigned to a variable first. We could have assigned that function to a variable, and that variable would have held the array as its value. In this example, we just passed the returned value to the var_dump() function so that it can be displayed on the screen.
If you’re familiar with other programming languages like Java, you may be familiar with specifying return types. PHP does have that capability too, but we’ll be covering that in a later article.