(PHP 4, PHP 5)
如果在一个函数中调用 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 的值)。
J.D. Grimes (2013-06-25 20:11:55)
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 fully executed.
Example:
a.php
<?php
include 'b.php';
foo();
?>
b.php
<?php
return;
function foo() {
echo 'foo';
}
?>
Executing a.php will output "foo".
MrLavender (2010-05-02 10:01:25)
@Radu
http://www.php.net/manual/en/language.operators.assignment.php
"The value of an assignment expression is the value assigned."
Note "the value assigned", not "the value assigned to".
The value assigned in the expression $a['e'] = 'sometxt' is 'sometxt', and that's what you're returning in function a().
Radu (2010-03-22 03:17:28)
When returning an array, you should declare the array before the return, else the result is not as you expect;
Watch this example:
<?php
function a(){
return $a['e'] = 'sometxt';
}
function b(){
$a['e'] = 'sometxt';
return $a;
}
function c(){
if(is_array(a())){
echo 'a is array';
}else{
echo 'a is NOT an array';
}
if(is_array(b())){
echo 'b is array';
}else{
echo 'b is NOT an array';
}
}
?>
This will print:
a is NOT an array
b is array
pgl at yoyo dot org (2009-07-31 08:23:19)
NB: using return to exit a command-line script will not use the return value as the script's return value. To do that, you need to use, eg, exit(1);
fyrye (2009-07-31 03:25:37)
A side note when you return a conditional value the variable type will inherit its type of Boolean
For example
<?php
function foo($SQL){
$conTemp = new mysqli("locahost", "root", "", "");
$conTemp->select_db("MyDB");
return $conTemp->query($SQL) or die("Query Failed!");
}
$result = foo("SELECT UserName FROM Users LIMIT 1");
echo gettype($result); //returns Boolean instead of object or die
?>
Instead be explicit with your function like so
<?php
function foo($SQL){
$conTemp = new mysqli("locahost", "root", "", "");
$conTemp->select_db("MyDB");
if(!$result = $conTemp->query($SQL)){
return die("Query Failed");
}
return $result;
}
$result = foo("SELECT UserName FROM Users LIMIT 1");
echo gettype($result); //Now will return Object or die
?>
list at regularoddity dot com (2008-10-07 09:26:23)
As obvious as it may seem, it might still be useful to point out that return called without any value returns null.
<?php
function test() {
return;
}
print gettype(test()) . "\n";
print (test()?'true':'false') . "\n";
print (!test()?'true':'false') . "\n";
print (test() === false?'true':'false') . "\n";
?>
This returns:
NULL
false
true
false
andrew at neonsurge dot com (2008-08-15 01:40:24)
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.
stoic (2008-06-06 06:21:36)
Just to clear things up, if using return on a global scope it will end EXECUTION but NOT PROCESSING.
for example:
file a.php
<?php
if(defined("A")) return;
define("A", true);
echo "Hello";
?>
file b.php
<?php
include("a.php");
include("a.php");
?>
will output "Hello" only once.
but if file a.php is
<?php
if(defined("A")) return;
define("A", true);
function foo(){
}
?>
running file b.php will produce error:
Fatal Error: Cannot redeclare foo()...
Denis.Gorbachev (2007-12-02 14:06:29)
direct true 0.59850406646729
direct false 0.62642693519592
indirect true 0.75077891349792
indirect false 0.73496103286743
It is generally more true, because indirect method implies creating additional variable and assigning a value to it.
But, you know, "results may vary".
mr dot xanadu at gmail dot com (2007-10-12 01:56:27)
I was wondering what was quicker:
- return a boolean as soon I know it's value ('direct') or
- save the boolean in a variable and return it at the function's end.
<?php
$times = 50000;
function return_direct ($boolean)
{
if ($boolean == true)
{
return true;
}
return false;
}
function return_indirect ($boolean)
{
$return = false;
if ($boolean == true)
{
$return = true;
}
return $return;
}
/* Direct, return true */
$time_start = microtime(true);
for ($i = 1; $i <= $times; $i++)
{
return_direct(true);
}
$time_end = microtime(true);
$time_direct_true = $time_end - $time_start;
/* Direct, return false */
$time_start = microtime(true);
for ($i = 1; $i <= $times; $i++)
{
return_direct(false);
}
$time_end = microtime(true);
$time_direct_false = $time_end - $time_start;
/* Indirect, return true */
$time_start = microtime(true);
for ($i = 1; $i <= $times; $i++)
{
return_indirect(true);
}
$time_end = microtime(true);
$time_indirect_true = $time_end - $time_start;
/* Direct, return false */
$time_start = microtime(true);
for ($i = 1; $i <= $times; $i++)
{
return_indirect(false);
}
$time_end = microtime(true);
$time_indirect_false = $time_end - $time_start;
echo "<pre>";
echo "direct true\t" . $time_direct_true;
echo "\ndirect false\t" . $time_direct_false;
echo "\nindirect true\t" . $time_indirect_true;
echo "\nindirect false\t" . $time_indirect_false;
echo "<pre>";
?>
Representative results:
direct true 0.163973093033
direct false 0.1270840168
indirect true 0.0733940601349
indirect false 0.0742440223694
Conclusion: saving the result in a variable appears to be faster. (Please note that my test functions are very simple, maybe it's slower on longer functions)
Spacecat (2007-07-24 18:13:07)
regardez this code:
print pewt( "hello!" );
function pewt( $arg )
{
include( "some_code.inc" );
}
some_code.inc:
return strtoupper( $arg );
.. after much hair pulling, discovered why nothing was being returned by the "some_code.inc" code in the function .. the return simply returns the result TO the function (giving the include function a value), not to the CALLING (print pewt). This works:
print pewt( "hello!" );
function pewt( $arg )
{
return include( "some_code.inc" );
}
So, RETURN works relative to block it is executed within.
warhog at warhog dot net (2005-12-18 12:28:44)
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".