PHP: return - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

require declare

Last updated: Fri, 26 Jan 2007

view this page in

# return

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.

For more information, see Returning values.

> Замечание: Note that since return() is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.

> Замечание: You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use `return ($a);` then you're not returning a variable, but the result of the expression `($a)` (which is, of course, the value of `$a`).

add a note User Contributed Notes
return
ufo at remove dot these dot parts dot quicknet dot nl
03-Mar-2007 10:38
`This works:
$a = 0;
$b = 2;
function foo()
{
$a or $a = $b or die ("arg");
}
This produces a syntax error:
$a = 0;
$b = 2;
function foo()
{
$a or $a = $b or return ("arg");
}
If it's not a bug, it's very counterintuitive!`
warhog at warhog dot net
18-Dec-2005 12: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".`
mike at uwmike dot com
06-Dec-2005 09:25
`If you have a class file that's getting out of control, you can set it up like so:
<?php
class myClass {
function do_this($a, $b) { return require(myClass_do_this.php); }
function do_that($a, $b, $c) { return require(myClass_do_that.php); }
}
?>
Might not be for everyone, but it's workable, readable, and keeps the source files shorter.`
PackCat
10-Jun-2005 06:02
`If you return a value that has not been set, or has been unset, the function will return that status instead. So,
<?php
$var1 = 1;
function unsetVar($var) {
unset($var);
return $var;
}
$var1 = unsetVar($var1); // this will unset $var1
?>
This is useful in situations where we want to conditionally proceed based on the output of a function; for example,
if (isset($fileListAsArray = functionGettingFileList())) {
// our list exists, do whatever
} else . . . // report error`
16-Feb-2003 03:04
`Use "exit()" to end all script execution for the current request. For HTTP requests, the response generated to that point will then be sent to the browser.`
add a note
require declare

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST