
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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
<?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
<?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
<?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
<?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
<?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
- Related Articles
- Returning two values from a function in PHP
- Returning Multiple Values in Python?
- Returning multiple values from a C++ function
- Returning values from a constructor in JavaScript?
- Function Returning Multiple Values in Go Language
- Returning array values that are not odd in JavaScript
- MongoDB query to limit the returning values of a field?
- MySQL always returning the bit values as blank? How to get the original values?
- Returning multiple values from a function using Tuple and Pair in C++
- PHP program to compare float values
- How to remove null values with PHP?
- PHP Pushing values into an associative array?
- Comparison of floating point values in PHP.\n
- Display array structure and values in PHP 7
- Returning an array in Java
