Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Pennsylvania, United States
    Posts
    54
    Thanks
    16
    Thanked 0 Times in 0 Posts

    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!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • Users who have thanked Fou-Lu for this post:

    johnnnn (05-05-2010)

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts

  • Users who have thanked MattF for this post:

    johnnnn (05-05-2010)

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    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(); 

  • Users who have thanked Phil Jackson for this post:

    johnnnn (05-05-2010)

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).
    Last edited by Fou-Lu; 05-03-2010 at 06:36 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    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(); 

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • #8
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    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(); 

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • #10
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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

  • Users who have thanked kbluhm for this post:

    johnnnn (05-05-2010)

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • #12
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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.
    Last edited by kbluhm; 05-04-2010 at 02:51 AM.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 

  • #14
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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...

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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'); 


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •