PHP return Statement


PHP return statement immediately terminates the execution of a function when it is called from within that function.

This function is also used to terminate the execution of an eval() function or script file.

If this function is called from a global scope, the function stops the execution of the current script. If the current script file was included using include() or required(), then control goes back to the calling file.


return expression 

Where expression is any valid expression. Note that, since the return is not a function but a language construct, using parenthesis is not required.

Return values

Condition Return value
If called from within a function Returns the argument of the function.
If the current script file was included using include() or required() Value given to return().
If no parameter is supplied NULL is returned.


Code of p.php

echo "p";

Code of q.php

echo "q";

View the output of p.php

View the output of q.php

See also

continue, break

Previous: declare statement
Next: include and require

PHP: Tips of the Day

PHP: What is <=> (the 'Spaceship' Operator) in PHP 7?

The <=> ("Spaceship") operator will offer combined comparison in that it will :

Return 0 if values on either side are equal
Return 1 if the value on the left is greater
Return -1 if the value on the right is greater

The rules used by the combined comparison operator are the same as the currently used comparison operators by PHP viz. <, <=, ==, >= and >. Those who are from Perl or Ruby programming background may already be familiar with this new operator proposed for PHP7.

//Comparing Integers

    echo 1 <=> 1; //output  0
    echo 3 <=> 4; //output -1
    echo 4 <=> 3; //output  1

    //String Comparison

    echo "x" <=> "x"; //output  0
    echo "x" <=> "y"; //output -1
    echo "y" <=> "x"; //output  1

Ref : https://bit.ly/2KvHmB1