Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Return Value

Return value is the value returned by a method or function call.

That value may be typed with a data type. In that case, each call to return must return that type.

It may also be typed with never, in case the method doesn’t return anything. In that case, it would never ends, throw an exception or kill the execution.

A value is returned with the keyword return. When no such command is used, for example when a method doesn’t have a return command and reach the end of the method’s body, then null is returned.

<?php

function trumpet() : int {
    return 1;
}

function forage() : never {
    throw new Exception("Error");
}

function migrate() : void {
    echo __METHOD__;
    // No return value
}

// returns null
function stampede() { }

?>

Documentation

See Also