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()?
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.
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).
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);
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);
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);
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.
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.
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...