Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-03-2010, 03:36 PM   PM User | #1
johnnnn
New Coder

 
Join Date: May 2009
Location: Pennsylvania, United States
Posts: 54
Thanks: 16
Thanked 0 Times in 0 Posts
johnnnn is an unknown quantity at this point
Question PHP: return vs exit

In the manual, if return; is called globally, it immediately end script execution. To my knowlege, exit() and die() will do this too. Is there any difference in return vs exit()?

Thanks!
johnnnn is offline   Reply With Quote
Old 05-03-2010, 03:56 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is a good question actually, and I can't do a thing to test this (and definitely have never tried in the past :P).
I believe the only difference between the two is in regards to their usage within registered shutdown functions scope. I'm pretty certain that if you have say 3 functions registered at shutdown that calling return in the first on the stack still executes the remaining functions; however, calling die/exit terminates and the remaining two functions do not execute.
From the main() itself, then no, I believe they function exactly the same. I'm fairly certain that the return on main still triggers shutdown events (it seems illogical if it does not), and would return a status code of 0 (if left null with return;), or !0 if not null to correspond to common return results.
This may be one worth a test.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
johnnnn (05-05-2010)
Old 05-03-2010, 04:30 PM   PM User | #3
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
http://uk3.php.net/manual/en/function.return.php#83663
MattF is offline   Reply With Quote
Users who have thanked MattF for this post:
johnnnn (05-05-2010)
Old 05-03-2010, 04:32 PM   PM User | #4
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
PHP Code:
<?php

function time_script_start(){
    
$mtime microtime();
       
$mtime explode(" ",$mtime);
       
$mtime $mtime[1] + $mtime[0];
       return 
$mtime;     
}
function 
time_script_end($starttime){
    
$mtime microtime();
       
$mtime explode(" ",$mtime);
       
$mtime $mtime[1] + $mtime[0];
       
$endtime $mtime;
       return (
$endtime $starttime);     
}

function 
test1($str){
    if(
$str===true){
        return 
true;    
    }else{
        exit(
true);        
    }
}
PHP Code:
$st time_script_start();
test1(true);
echo 
"<p>runtime: " time_script_end($st) . "</p>"
Code:
runtime: 2.8848648071289E-5
PHP Code:
$st time_script_start();
test1(false);
echo 
"<p>runtime: " time_script_end($st) . "</p>"
Code:
1
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 
Phil Jackson is offline   Reply With Quote
Users who have thanked Phil Jackson for this post:
johnnnn (05-05-2010)
Old 05-03-2010, 06:33 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Don't return/exit against the test1 with a value:
PHP Code:
function test1($str){
    if(
$str===true){
        return;    
    }else{
        exit;        
    }

Also, I was sort of indicating shutdown registration:
PHP Code:
function time_script_start(){
    return 
microtime(true);   
}
function 
time_script_end($starttime){
    
$mtime microtime(true);
    
printf("End time: %0.5f\n", ($mtime $starttime));     
}

function 
test1($str){
    if(
$str===true){
        return;    
    }else{
        exit;        
    }


$useReturn true;
$start time_script_start();
// Two tests, one with true, one with false.
register_shutdown_function('test1'$useReturn);
register_shutdown_function('time_script_end'$start);

printf("Start: %0.5f\n"$start);

?> 
Try that swapping that $useReturn true and false. I'd expect that the return against a standard function call versus the exit would differ since the return will push back up the stack, but I'm really curious if the same is true with the shutdown_functions.
If it works as expected (and I believe the return will continue), testing with 'true' should output:
Start: somefloat
End time: somefloat

And with false:
Start: somefloat

Can you test that one out?

Edit:
Oh oh, and also, once done can you also add a return; before the ?> but after the register_shutdown_function? I want to be certain that the return in main() still triggers shutdown events (I assumed it would).
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 

Last edited by Fou-Lu; 05-03-2010 at 06:36 PM..
Fou-Lu is offline   Reply With Quote
Old 05-03-2010, 07:14 PM   PM User | #6
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
PHP Code:
<?php

function time_script_start(){ 
    return 
microtime(true);    

function 
time_script_end($starttime){ 
    
$mtime microtime(true); 
    
printf("End time: %0.5f\n", ($mtime $starttime));      


function 
test1($str){ 
    if(
$str===true){ 
        return;     
    }else{ 
        exit;         
    } 
}  

$useReturn true
$start time_script_start(); 
// Two tests, one with true, one with false. 
register_shutdown_function('test1'$useReturn); 
register_shutdown_function('time_script_end'$start); 

printf("Start: %0.5f<br />\n"$start); 

?>
true:

Code:
Start: 1272910410.66400
End time: 0.00024
false:

Code:
Start: 1272910462.19000
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 
Phil Jackson is offline   Reply With Quote
Old 05-03-2010, 09:11 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by Phil Jackson View Post
PHP Code:
<?php

function time_script_start(){ 
    return 
microtime(true);    

function 
time_script_end($starttime){ 
    
$mtime microtime(true); 
    
printf("End time: %0.5f\n", ($mtime $starttime));      


function 
test1($str){ 
    if(
$str===true){ 
        return;     
    }else{ 
        exit;         
    } 
}  

$useReturn true
$start time_script_start(); 
// Two tests, one with true, one with false. 
register_shutdown_function('test1'$useReturn); 
register_shutdown_function('time_script_end'$start); 

printf("Start: %0.5f<br />\n"$start); 

?>
true:

Code:
Start: 1272910410.66400
End time: 0.00024
false:

Code:
Start: 1272910462.19000
Great thats perfect. So return still follows it standard convention when using a stack scope.
Did you have a chance to test it by adding return; in the main()?
PHP Code:
<?php

function time_script_start(){ 
    return 
microtime(true);    

function 
time_script_end($starttime){ 
    
$mtime microtime(true); 
    
printf("End time: %0.5f\n", ($mtime $starttime));      


function 
test1($str){ 
    if(
$str===true){ 
        return;     
    }else{ 
        exit;         
    } 
}  

$useReturn true
$start time_script_start(); 
// Two tests, one with true, one with false. 
register_shutdown_function('test1'$useReturn); 
register_shutdown_function('time_script_end'$start); 

printf("Start: %0.5f<br />\n"$start); 
return;
?>
Just with $useReturn true, I'm curious if that still triggers shutdown events (I really hope it does >.<).
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-03-2010, 10:01 PM   PM User | #8
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
Sorry, Fou-lu, watching snooker!

PHP Code:
<?php


function time_script_start(){  
    return 
microtime(true);     
}  
function 
time_script_end($starttime){  
    
$mtime microtime(true);  
    
printf("End time: %0.5f\n", ($mtime $starttime));       
}  

function 
test1($str){  
    if(
$str===true){  
        return;      
    }else{  
        exit;          
    }  
}   

$useReturn true;  
$start time_script_start();  
// Two tests, one with true, one with false.  
register_shutdown_function('test1'$useReturn);  
register_shutdown_function('time_script_end'$start);  

printf("Start: %0.5f<br />\n"$start);  
return; 

?>
True:

Code:
Start: 1272920406.01300
End time: 0.00011
False:
Code:
Start: 1272920475.35800
__________________
Website Design Mansfield
PHP Code:
function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
echo 
I_LOVE(); 
Phil Jackson is offline   Reply With Quote
Old 05-03-2010, 11:02 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Sweet, thanks for testing mate.
So yeah, in a main() scope the return is identical to exit in every way.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-03-2010, 11:30 PM   PM User | #10
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
If you `return` in a nested file, it will halt the execution of that file only, but still continue on into the parent file.

You can optionally return a value from an included file:

whatever.php
PHP Code:
<?php

return 'kbluhm';

$name 'not kbluhm';
index.php
PHP Code:
<?php

$name 
= include './whatever.php';

echo 
$name// kbluhm
__________________
ZCE
kbluhm is offline   Reply With Quote
Users who have thanked kbluhm for this post:
johnnnn (05-05-2010)
Old 05-03-2010, 11:51 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yes thats right; best I know exit within an include does actually just kill the include much like the return, but I don't believe that its actually assignable. And those would both be considered main.
So a situation where its not the same. Interesting.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-04-2010, 02:44 AM   PM User | #12
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Quote:
Originally Posted by Fou-Lu View Post
Yes thats right; best I know exit within an include does actually just kill the include much like the return, but I don't believe that its actually assignable. And those would both be considered main.
So a situation where its not the same. Interesting.
`exit` will halt everything. `return` just halts the file or function in which it was called.
__________________
ZCE

Last edited by kbluhm; 05-04-2010 at 02:51 AM..
kbluhm is offline   Reply With Quote
Old 05-04-2010, 04:34 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by kbluhm View Post
`exit` will halt everything. `return` just halts the file or function in which it was called.

I'm embarassed; I should have known better. What is the point to die if you cannot die? Lol.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 05-04-2010, 10:47 PM   PM User | #14
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,503
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Quote:
Originally Posted by Fou-Lu View Post

I'm embarassed; I should have known better. What is the point to die if you cannot die? Lol.
Haha... well `return` and die() are not the same. die() is simply an alias for exit(), so die() and exit() are identical in every way. If you die(), you die...
__________________
ZCE
kbluhm is offline   Reply With Quote
Old 05-04-2010, 10:59 PM   PM User | #15
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,738
Thanks: 4
Thanked 2,464 Times in 2,433 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by kbluhm View Post
If you die(), you die...
That looks like a new sig entry to me
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Tags
die, exit, php, return

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:06 AM.


Advertisement
Log in to turn off these ads.