MyBB Community Forums

Full Version: Do you have a favorite 'exit'? Why?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
<?php
//exit program normally
exit;
exit();
exit(0);

//exit with an error code
exit(1);
exit(0376); //octal
?>
~source

Is there any difference between these two 'exit;' vs. 'exit();'
I'm guessing no difference, but curious to know...
Why would anyone bother to use 'exit();' if 'exit;' does the same thing? Toungue
Do you have a favorite 'exit'? Why?

exit is a language construct. It makes no difference if you use exit or exit() it just depends on the developer.

Personally I use exit but other developers might like to be consistent and use exit() along side exit('Some message').

exit is also an alias of die (I tend to use die when outputting error messages and use exit when I want the script to generally exit)
I usually just use die() but sometimes exit; Smile
I prefer "exit;"

Though, you shouldn't rely too much on that, it's better to do :

<?php
if($var == "something")
{
//do something
}
else
{
//do something else
}
?>

Then :

<?php
if($var == "something")
{
//do something
exit;
}
//do something else
?>
Personally, unless there's some kind of exceptional situation, I just let execution run off the end and return normally. Of course, almost all of the PHP I've written was for MyBB so exit() would mess things up, but even in normal C/C++ programs I just return from main to end the program.
I use exit(), which is for consistency as Nathan stated. I use exit(); for exiting and die("Some Message"); for error messaging... but I often use:
echo "<pre>";print_r($array_var);die("</pre>");
for debugging insertions too Toungue
I use exit(); myself...not sure why I just always preferred that to just exit;.

I do of course use die() too but in terms of just exit options I like exit();.
BTW, Would..
//exit with an error code (for example)
exit(123);
...by default, log the error in the Apache logs (assuming you normally see PHP errors there) or do you need to add the instruction to log?

EDIT:
Checked here and didn't see much use of the error code.

<off topic>
Quote:Don't use the exit() function in the auto prepend file with fastcgi (linux/bsd os).
It has the effect of leaving opened files with for result at least a nice "Too many open files ..." error.
</off topic>


EDIT #2
Just tested and don't see it in the Apache log.
How do you ever use that error code?, since the script has stopped... Toungue