|
returnIf 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 For more information, see Returning values.
Note:
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.
Note:
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 Related Examples ( Source code ) » return Examples ( Source code ) » Send email with CC and BCC Examples ( Source code ) » Form value validation Examples ( Source code ) » Form Data Validation With Error Count Examples ( Source code ) » Login form with Error Messages and Preserving User Input Examples ( Source code ) » The Table and HTMLTable Classes Examples ( Source code ) » The Table Class Examples ( Source code ) » Finding the Number of Rows Returned by a SELECT Statement with mysql_num_rows() Examples ( Source code ) » Use mysql_result function to get query result Examples ( Source code ) » Get returned row count Examples ( Source code ) » Total number of fields in a table Examples ( Source code ) » Initiate session and create a few session variables Examples ( Source code ) » Calculate the size for a directory Examples ( Source code ) » Is it a file Examples ( Source code ) » Is the file a directory Examples ( Source code ) » Is the file readable Code Examples / Notes » returnspacecat
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. mike
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. mr dot xanadu
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) warhog
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". |