Understanding Return in PHP

Hi, today we’ll discuss the use of the return statement in PHP.

The return statement is often used when making a function on the procedural programming or method on object oriented programming.

When used, the execution of the function will be terminated and the value sends back (return) to the caller of the function. To have a better understanding of the return statement, consider the following example:

// Create function
function multiply($a, $b)
	$result = $a * $b;
	return $result;

// Call the function
$result = multiply(3, 4);
echo $result; // Output: 9

// Call the function
$a = 4;
$b = 5;
echo 'The result of ' . $a . ' x ' . $b . ' is: ' . multiply ($a, $b); // The result of 4 x 5 is: 20

On the multiplication function above, we store the multiplication result of $a and $b variables to the $result variable. Furthermore, the value of the $result variable send back (return) to the caller.

On the line 10: When we call the function by giving arguments 3 and 4 ( $result = multiply(3, 4) ) then the result ( that is 12) will be returned (return) to that function call, which is then stored to the $result variable.

The illustration looks like the following:

Understanding Return in PHP - Illustration

Local and Global Variables

Note that we create variables named $results both inside and outside the function, both variables do not collide because each other has its own scope.

The $result variable within the function only works inside the function, whereas the $result variable outside the function applies only outside the function (global variable).

Functions can not use global variables, as well as global can not use variables within the function.

return Statement to Immediately Terminate Function Execution

In the real world, it’s a good practice to use return statement to terminate the execution of the function, even in the beginning of the function.

For example, let’s create a similar function to the previous, but now, it accept a new argument, we named the function operation

function operation($a, $b, $type)
	if ($type == 'x') {
		return $a * $b;
	} elseif ($type == '+') {
		return $a + $b;
	} else {

echo operation(2, 3, 'x'); // Output: 6
echo operation(4, 3, '+'); // Output: 7
echo operation(4, 2, '-'); // Output: empty

In the above example, when we call the operation function with an argument of x, then the execution of the function will be terminated on line 5 return $a * $b, and for +, terminated on line 7.

If the third argument is not + or x then the function does not return any results.

The differences Between Return and Echo in PHP

Maybe you wondered, then what the difference between return and echo? according to the above example, the echo is used only for print output.

So if we replace the return with echo, the function will not give any value (doesn’t return anything), for example, let’s modify the multiply function that we have created by changing return into echo.

// Create function
function multiply($a, $b)
	$result = $a * $b;
	echo $result;

$result = multiply(3, 4); // output: 12
echo 'Result: ' . $result; // output: Result:

In the above example, when we call the function by giving arguments of 3 and 4, it will immediately print output of 12.

When we save the result of the function to the $result variable, then the variable does not contain anything, because, the function does not provide any return, so if we print the $result variable, we don’t see any output

In this tutorial, we have learned return statement in PHP.

Related Post

Leave a comment