PHP
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: Sun, 25 Nov 2007

view this page in

return

Αν κληθεί μέσα από μια συνάρτηση, η δήλωση return() αμέσως τερματίζει την εκτέλεση της τρέχουσας συνάρτησης, και επιστρέφει την παράμετρο της ως τιμή της συνάρτησης που κλήθηκε. Η return() θα τερματίσει επίσης και την εκτέλεση μιας eval() συνάρτησης του script αρχείου.

Αν κληθεί από περιοχή με global εμβέλεια, τότε η εκτέλεση του τρέχοντος script του αρχείου τερματίζει. Αν το τρέχον script του αρχείου ήταν include()ed ή require()ed, τότε ο έλεγχος περνάει πάλι στο αρχείου που εκτελεί την κλήση. Επιπλέον, αν το τρέχον script του αρχείου ήταν include()ed (συμπεριελαμβανόταν), τότε η τιμή που δίνεται στη return() θα επιστρέφεται ως η τιμή της κλήσης της include(). Αν η return() καλείται από το κυρίως script του αρχείου, τότε η εκτέλεση του script τερματίζει. Αν το τρέχον script του αρχείου πήρε το όνομα από την auto_prepend_file ή την auto_append_file επιλογές για configuration στο php.ini, τότε η εκτέλεση του script του αρχείου τερματίζει.

Για περισσότερες πληροφορίες, δείτε Επιστρέφοντας τιμές.

Note: Σημειώστε ότι αφού η return() είναι μια γλωσσική δομή και όχι μια συνάρτηση, οι παρενθέσεις που περικλείουν τις παραμέτρους της δεν απαιτούνται--στην πραγματικότητα, συχνά μένουν εκτός και δεν χρησιμοποιούνται, παρόλο που δεν έχει σημασία αν θα κάνουμε το ένα ή το άλλο.



require> <declare
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
return
stoic
06-Jun-2008 06:21
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
02-Dec-2007 02:06
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
12-Oct-2007 01:56
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
24-Jul-2007 06:13
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
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".

require> <declare
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites