Returning values

Values are returned by using the optional return statement. Any
type may be returned, including arrays and objects. This causes the
function to end its execution immediately and pass control back to
the line from which it was called. See return
for more information.

Return type declarations

PHP 7 adds support for return type declarations. Similarly to
argument type declarations,
return type declarations specify the type of the value that will be
returned from a function. The same
types
are available for return type declarations as are available for argument
type declarations.

Strict typing
also has an effect on return type declarations. In the default weak mode,
returned values will be coerced to the correct type if they are not
already of that type. In strong mode, the returned value must be of the
correct type, otherwise a TypeError will be thrown.

Note:

When overriding a parent method, the child's method must match any return
type declaration on the parent. If the parent doesn't define a return
type, then the child method may do so.

Examples

Example #4 Basic return type declaration

<?phpfunction sum($a, $b): float { return $a + $b;}

// Note that a float will be returned.var_dump(sum(1, 2));?>

The above example will output:

float(3)

Example #5 Strict mode in action

<?phpdeclare(strict_types=1);

function sum($a, $b): int { return $a + $b;}

var_dump(sum(1, 2));var_dump(sum(1, 2.5));?>

The above example will output:

int(3)
Fatal error: Uncaught TypeError: Return value of sum() must be of the type integer, float returned in - on line 5 in -:5
Stack trace:
#0 -(9): sum(1, 2.5)
#1 {main}
thrown in - on line 5

Developers with a C background may expect pass by reference semantics for arrays. It may be surprising that pass by value is used for arrays just like scalars. Objects are implicitly passed by reference.

<?php

# (1) Objects are always passed by reference and returned by reference

class Obj { public $x;}

function obj_inc_x($obj) {$obj->x++; return $obj;}

$obj = new Obj();$obj->x = 1;

$obj2 = obj_inc_x($obj);obj_inc_x($obj2);

print $obj->x . ', ' . $obj2->x . "\n";

# (2) Scalars are not passed by reference or returned as such

function scalar_inc_x($x) {$x++; return $x;}

$x = 1;

$x2 = scalar_inc_x($x);scalar_inc_x($x2);

print $x . ', ' . $x2 . "\n";

# (3) You have to force pass by reference and return by reference on scalars

function &scalar_ref_inc_x(&$x) {$x++; return $x;}

$x = 1;

$x2 =& scalar_ref_inc_x($x); # Need reference here as well as the function sigscalar_ref_inc_x($x2);

print $x . ', ' . $x2 . "\n";

# (4) Arrays use pass by value sematics just like scalars

function array_inc_x($array) {$array{'x'}++; return $array;}

$array = array();$array['x'] = 1;

$array2 = array_inc_x($array);array_inc_x($array2);

print $array['x'] . ', ' . $array2['x'] . "\n";

# (5) You have to force pass by reference and return by reference on arrays

function &array_ref_inc_x(&$array) {$array{'x'}++; return $array;}

$array = array();$array['x'] = 1;

$array2 =& array_ref_inc_x($array); # Need reference here as well as the function sigarray_ref_inc_x($array2);

Functions which return references, may return a NULL value. This is inconsistent with the fact that function parameters passed by reference can't be passed as NULL (or in fact anything which isnt a variable).