Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a simple `return` in a PHP function simply end the function prematurely?

Tags:

function

php

I have just seen this

// Check to see if the request is a XHR call    
if (request::is_ajax())
{
  // Send the 403 header
  header('HTTP/1.1 403 Forbidden');
  return;
}

I have not seen a simple return before, and I have never used it. My only guess is that it simply acts the same as any return 'something' (halting the function), except doesn't actually return a result.

Furthermore, what would happen in this situation?

function text($var)   
{
    if ( ! $var) {
        return;
    }
    do_something();
}

$var = text('');

I know it's a bad example (it should probably return false or throw an exception), but would it be an error, or would the $var simply be null or blank?

like image 902
alex Avatar asked Jul 11 '09 15:07

alex


People also ask

Does return end a function PHP?

The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.

What does the return function do in PHP?

The purpose of return statement in PHP is to return control of program execution back to the environment from which it was called. Upon returning, execution of expression following the one which invooked other function or module.

How do you end a function in PHP?

The exit() function in PHP is an inbuilt function which is used to output a message and terminate the current script. The exit() function only terminates the execution of the script. The shutdown functions and object destructors will always be executed even if exit() function is called.

What happens if you don't return a function?

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.


2 Answers

Yes, "return;" or return(); in a function stops running that function (or file if the return statement is not in a function) at that point (if the code is executed) and technically returns a null value.

Your first example would output the 'HTTP/1.1 403 Forbidden' header and then end that function if request::is_ajax() equals true.

In your code example:

function text($var) 
{

    if ( ! $var) {
        return;
    }
    do_something();

}
$var = text('');

You can have the following out comes:

  • If $var is NOT TRUE (i.e. false, null, text or an integer), then exit function and return null.
  • If $var is EXACTLY TRUE (the if statement is checking if $var is anything but true), then run do_something();

I would suspect you probably want to change the if statement to either:

if (!is_string($var)) {

if you just want to do_something() to a string or

if (is_null($var)) {

if you only want to do_something() to $var if it's been set. You may also want to change your function declaration to: function text($var==NULL) { so if you call text without a paramter, $var is automatically set to null.

like image 183
Richy B. Avatar answered Oct 17 '22 04:10

Richy B.


A return with no value returns null.

More information from the manual:

If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call. return() will also end the execution of an eval() statement or script file.

If called from the global scope, then execution of the current script file is ended. If the current script file was include()ed or require()ed, then control is passed back to the calling file. Furthermore, if the current script file was include()ed, then the value given to return() will be returned as the value of the include() call. If return() is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.

like image 21
mattphp Avatar answered Oct 17 '22 02:10

mattphp