Current time: 04-23-2014, 02:39 PM Hello There, Guest! (LoginRegister)


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do you have a favorite 'exit'? Why?
02-25-2012, 04:41 AM
Post: #1
Do you have a favorite 'exit'? Why?
PHP Code:
<?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?

#
MyBB is the best forum software! Exclamation
Find all posts by this user
Quote this message in a reply
02-25-2012, 05:09 AM
Post: #2
RE: 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)
Visit this user's website Find all posts by this user
Quote this message in a reply
02-25-2012, 07:42 AM
Post: #3
RE: Do you have a favorite 'exit'? Why?
I usually just use die() but sometimes exit; Smile

Quote:Golf and alcohol don't mix, that's why I don't drink and drive!
Video Tutorials: Installing MyBB | Upgrading MyBB
Visit this user's website Find all posts by this user
Quote this message in a reply
02-25-2012, 05:14 PM (This post was last modified: 02-25-2012 05:15 PM by TheGarfield.)
Post: #4
RE: Do you have a favorite 'exit'? Why?
I prefer "exit;"

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

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

Then :

PHP Code:
<?php
if($var == "something")
{
//do something
exit;
}
//do something else
?>

Sorry seems to be the hardest word!
Find all posts by this user
Quote this message in a reply
02-26-2012, 01:59 AM
Post: #5
RE: Do you have a favorite 'exit'? Why?
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.

[Image: rslm.php]
Visit this user's website Find all posts by this user
Quote this message in a reply
02-26-2012, 02:12 AM
Post: #6
RE: Do you have a favorite 'exit'? Why?
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

CodeWeavers - CrossOver: Windows Compatibility on Macintosh and Linux
[Image: Advocate_125.png]
Doesn't like unsolicited PMs
Find all posts by this user
Quote this message in a reply
02-26-2012, 02:40 AM
Post: #7
RE: Do you have a favorite 'exit'? Why?
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();.

/My Plugins/
Find all posts by this user
Quote this message in a reply
02-29-2012, 11:51 AM (This post was last modified: 02-29-2012 12:01 PM by seeker.)
Post: #8
RE: Do you have a favorite 'exit'? Why?
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

#
MyBB is the best forum software! Exclamation
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

Contact Us | MyBB | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication