PHP Keywords: return

20 February 2019 - 6:23am

Welcome to my series on every PHP keyword and its usage. Today's item: return.

The return keyword ends the current scope, and optionally passes a value back to the calling scope. It can be used inside of functions/methods, and in the global scope.

When used inside a function, the function will finish and the result will be passed back to the caller.

When used inside the global scope, it will end the current script. If the script was included from another script, the result will be passed back to the calling script.

Usage

<?php
function return7()
{
    return 7;
}

$seven = return7();

return $seven;