PHP Returning values


Introduction

A function can have return as last statement in its body although it is not mandatory. When a function is called, control of program come back to calling environment after executing statements in its body block - irrespective of whether last statement in function block is return or not. In absence of retun statement, control returns NULL value to caller. If return statement consistes of an expression clause, value of expression is returned. Function can return only one value which may be of scalar type, array, or an object. Returned value may be assigned to some variable for subsequent processing

function with return

In following example, a function returns sum of two integers passed as argument

Example

 Live Demo

<?php
function add($var1, $var2){
   $var3= $var1+$var2 ;
   return $var3;
}
$x=10;
$y=20;
$z=add($x,$y);
echo "addition=$z";
?>

Output

This will produce following result. −

addition=30

Returning array

Function can return only one value. However, array of multiple values can be returned. Following example passes two numbers to a function that returns array of addition, subtraction, multiplication and division

Example

 Live Demo

<?php
function result($var1, $var2){
   $r1=$var1+$var2;
   $r2=$var1-$var2;
   $r3=$var1*$var2;
   $r4=$var1/$var2;
   return array("add"=>$r1,"sub"=>$r2,"multiply"=>$r3,"division"=>$r4);
}
$x=10;
$y=20;
$arr=result($x,$y);
foreach ($arr as $k=>$v){
   echo $k . "->" . $v . "
"; } ?>

Output

This will produce following result. −

add->30
sub->-10
multiply->200
division->0.5

Return by reference

Just as arguments can be passed by reference, a function can return by reference also. For that purpose, function's name must be prefixed by $ symbol. Further, & symbol must also be given in function call

In following example, myfunction() has a static array. One of its elements is returned by reference and is accepted in a variable. Value of variable is then modified and same function is called again. Array in the function should now show its value changed.

Example

 Live Demo

<?php
function &myfunction(){
   static $arr=[1,2,3,4,5];
   echo "array elements: ";
   foreach ($arr as $i){
      echo "$i ";
   }
   echo "
";    return $arr[2]; } $var=&myfunction(); echo "returned by reference : $var
"; $var=100; $var=&myfunction(); ?>

Output

This will produce following result. −

array elements: 1 2 3 4 5
returned by reference : 3
array elements: 1 2 100 4 5

Values of variables $x and $y are interchanged in swap() function. Since, variables are passed by reference, the variables show modified values outside the function too

return type hints (return Type declarations)

From PHP 7 onwards, you can specify type hints for returned variable/object, just as it is possible to declare type for arguments. For return type also all scalar types, class and array can be used

Syntax

//define a function with type hints for return value
function myfunction($arg1, $arg2): type{
   ..
   ..
   return $var;
}

All standard PHP data types including scalar types, array, class/interface, iterable and object are valid types for providing type hints for return variable in a function declaration

Example

 Live Demo

<?php
function add($x, $y): float{
   return $x+$y;
}
$var=add(5,8);
var_dump($var);
?>

Output

This will produce following result. −

float(13)

Use of declare statement with strict_types=1 will prevent coercion of data types

Example

 Live Demo

<?php
declare (strict_types=1);
function add($x, $y): int{
   return $x+$y;
}
$var=add(5.5,8.8);
var_dump($var);
?>

Output

This will now throw exception as follows −

PHP Fatal error: Uncaught TypeError: Return value of add() must be of the type integer, float returned

Updated on: 18-Sep-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements