return

(PHP 4, PHP 5, PHP 7)

如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。return 也会终止 eval() 语句或者脚本文件的执行。

如果在全局范围中调用,则当前脚本文件中止运行。如果当前脚本文件是被 include 的或者 require 的,则控制交回调用文件。此外,如果当前脚本是被 include 的,则 return 的值会被当作 include 调用的返回值。如果在主脚本文件中调用 return,则脚本中止运行。如果当前脚本文件是在 php.ini 中的配置选项 auto_prepend_file 或者 auto_append_file 所指定的,则此脚本文件中止运行。

更多信息见返回值

Note: 注意既然 return 是语言结构而不是函数,因此其参数没有必要用括号将其括起来。通常都不用括号,实际上也应该不用,这样可以降低 PHP 的负担。

Note: 如果没有提供参数,则一定不能用括号,此时返回 NULL。如果调用 return 时加上了括号却又没有参数会导致解析错误。

Note: 当用引用返回值时永远不要使用括号,这样行不通。只能通过引用返回变量,而不是语句的结果。如果使用 return ($a); 时其实不是返回一个变量,而是表达式 ($a) 的值(当然,此时该值也正是 $a 的值)。

User Contributed Notes

rupaldhamesha at hotmail dot com 12-Jan-2017 07:12
I am facing problem with return.

I have one php file say welcome.php
  
in that I am including
   uerChecking - this is a class file
   header.html file

in header.html file I have included one user.php file
  thisss will display user details.

Here control is not getting transfer from user.php to header.html file

I made this header.html file header.php file but then also control is not getting transfered.
hairbysubaru at gmail dot com 29-Aug-2016 02:48
<?php

$stat
= array("Inactive","Active");

function
fred($z)
{
   
$a = "firstname";
   
$b = "lastname";
   
$status=0;
    return array(
$a,$b,$z,$status);
}
$results = list($ra,$rb,$rc,$status) = fred("user_details");
//what we get...
print_r($results);
// interpreted results, knowing the 'structure' of $results.
echo "context value 2(heading):".$results[2];
echo
"context value 0(user fname):".$results[0];
echo
"context value 1(user lname):".$results[1];
echo
"context value 3(status):".$results[3];
echo
"status:".$stat[$results[3]];

?>

Produces...

Array ( [0] => firstname [1] => lastname [2] => user_details [3] => 0 )

context value 0(heading):user_details
context value 1(user fname):firstname
context value 2(user lname):lastname
context value 3(status):0
status:Inactive

Context is everything.
sebi at sebi dot moe 27-Jul-2016 11:16
One situation where returning a value from a script and using it could be considered a good use is config files. Zend Framework 2 promotes this structure for this purpose, and it works pretty well.

Example config file:

<?php
return [
 
'site' => 123,
 
'settings' => 'asd'
];

Reading:

<?
php
$config
= (require 'config.php');
// Or
$config = array_merge(require 'config/local.php', require 'config/application.php');
brad dot k dot harms at gmail dot com 14-Jul-2015 07:06
I wonder if return in the global scope would be a semantic way to store low-level, pure-PHP config arrays while avoiding polluting the global scope. For example:

<?php
// app.php

function app() {
   
$conf = include('conf.php');
}

app();

<?
php
// conf.php

return [
   
'db' => [
       
'host' => '127.0.0.1',
       
'username' => 'user',
       
'password' => 'pass'
   
],
];

?>

This approach produces exactly 0 global variables.

(PS. I also tried this using a JS-like module pattern using an anonymous function but it seems that an anon function can't be defined and called in the same expression.)
Russell Weed 14-Feb-2015 01:09
Following up on Tom and warhog's comments regarding using return in global scope, here is another reason not to do it:

For command line scripts, the return statement will NOT return a status to the OS!

<?php
if ($somethingBad)
   return
42; // This will not work! You'll get 0 back instead!
else
   return
0;
?>

Instead, you must use exit(): http://php.net/exit

<?php
if ($somethingBad)
   exit(
42); // OS will receive (int) 42 as the return code
else // everything is fine
  
exit(0);
?>
Anonim 13-Jan-2015 04:58
Also note, what you cannot do anything after  the 'return' usage in function.
For example:

<?php
$_SESSION
['text'] = 'Best PHP';
function
getText()
{
   
$text = $_SESSION['text'];
    return
$text;
    unset(
$_SESSION['text']);
}
echo
getText().'<br />';
echo
$_SESSION['text'];
?>

This will output:
Best PHP
Best PHP

Twice, because we have used unset() function after 'return'.
Tom 05-Feb-2014 07:35
Keep in mind that even if PHP allows you to use "return" in the global scope it is very bad design to do so.

Using the return statement in the global scope encourages programmers to use files like functions and treat the include-statement like a function call. Where they initialize the file's "parameters" by setting variables in the global scope and reading them in the included file.

Like so: (WARNING! This code was done by professionals in a controlled environment. Do NOT try this at home!)
<?php
$parameter1
= "foo";
$parameter2 = "bar";
$result = include "voodoo.php";
?>

Where "voodoo.php" may be something like:
<?php
return $parameter1 . " " . $parameter2;
?>

This is one of the worst designs you can implement since there is no function head, no way to understand where $parameter1 and $parameter2 come from by just looking at "voodoo". No explanation in the calling file as of what $parameter1 and -2 are doing or why they are even there. If the names of the parameters ever change in "voodoo" it will break the calling file. No IDE will properly support this very poor "design". And I won't even start on the security issues!

If you find yourself in a situation where a return-statement in global scope is the answer to your problem, then maybe you are asking the wrong questions. Actually you may be better off using a function and throwing an exception where needed.

Files are NOT functions. They should NOT be treated as such and under no circumstances should they "return" anything at all.

Remember: Every time you abuse a return statement God kills a kitten and makes sure you are reborn as a mouse!
J.D. Grimes 26-Jun-2013 05:11
Note that because PHP processes the file before running it, any functions defined in an included file will still be available, even if the file is not executed.

Example:

a.php
<?php
include 'b.php';

foo();
?>

b.php
<?php
return;

function
foo() {
     echo
'foo';
}
?>

Executing a.php will output "foo".
andrew at neonsurge dot com 15-Aug-2008 10:40
Response to stoic's message below...

I believe the way you've explained this for people may be a bit confusing, and your verbiage is incorrect.  Your script below is technically calling return from a global scope, but as it says right after that in the description above... "If the current script file was include()ed or require()ed, then control is passed back to the calling file".  You are in a included file.  Just making sure that is clear.

Now, the way php works is before it executes actual code it does what you call "processing" is really just a syntax check.  It does this every time per-file that is included before executing that file.  This is a GOOD feature, as it makes sure not to run any part of non-functional code.  What your example might have also said... is that in doing this syntax check it does not execute code, merely runs through your file (or include) checking for syntax errors before execution.  To show that, you should put the echo "b"; and echo "a"; at the start of each file.  This will show that "b" is echoed once, and then "a" is echoed only once, because the first time it syntax checked a.php, it was ok.  But the second time the syntax check failed and thus it was not executed again and terminated execution of the application due to a syntax error.

Just something to help clarify what you have stated in your comments.
warhog at warhog dot net 18-Dec-2005 09:28
for those of you who think that using return in a script is the same as using exit note that: using return just exits the execution of the current script, exit the whole execution.

look at that example:

a.php
<?php
include("b.php");
echo
"a";
?>

b.php
<?php
echo "b";
return;
?>

(executing a.php:) will echo "ba".

whereas (b.php modified):

a.php
<?php
include("b.php");
echo
"a";
?>

b.php
<?php
echo "b";
exit;
?>

(executing a.php:) will echo "b".