[RESOLVED] do I need an else if I'm using exit?
Results 1 to 5 of 5

Thread: [RESOLVED] do I need an else if I'm using exit?

  1. #1
    Closet Nice Guy schwim's Avatar
    Join Date
    Jun 2006
    Location
    State of confusion
    Posts
    685

    resolved [RESOLVED] do I need an else if I'm using exit?

    Hi there folks!

    I've always written in the following manner:

    PHP Code:

    if($if_its_safe){

        
    /* Do the stuff */

    }else{

        
    /* Don't do the stuff.


    but have always come across:

    PHP Code:

    if(!$its_safe){

       exit;

    }

    /* Do the stuff only if it's safe */ 
    I've never written that way because visually, the super secret stuff is only an "exit;" away from the checks and balances. It's worried me.

    It looks much neater, however so I wouldn't mind moving to that. Is it a safe way to handle things? I know this can be a loaded question, but I'm only asking if exiting a script really exits the script or if there's any added security in burying my stuff in an else.

    Thanks for your time!
    Warning: If you are taking advice from me, then you are in really bad shape. Let's both hope for your code's sake that someone comes along and pulls us out of this fix that I've gotten us into.

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,864
    You don't need it in the syntactic sense of course. Personally I would go with the latter, since it saves a level of indentation that doesn't provide any useful information through the rest of the code; "if you're here then you didn't exit - since you didn't exit I thought you'd like to know that you're still here."

    Quote Originally Posted by schwim
    I'm only asking if exiting a script really exits the script
    Yes.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Closet Nice Guy schwim's Avatar
    Join Date
    Jun 2006
    Location
    State of confusion
    Posts
    685
    Thanks a bunch for your help, weed, I'll handle it that way from here on out, as I like the look of it better.

    Thanks again!
    Warning: If you are taking advice from me, then you are in really bad shape. Let's both hope for your code's sake that someone comes along and pulls us out of this fix that I've gotten us into.

  4. #4
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,774
    FYI the same is true of a return statement in a function. IE:

    PHP Code:
    function foo($var) {
       if( 
    $var === BAR ) {
          return 
    false;
       }
       return 
    true;

    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  5. #5
    Closet Nice Guy schwim's Avatar
    Join Date
    Jun 2006
    Location
    State of confusion
    Posts
    685
    Quote Originally Posted by Derokorian View Post
    FYI the same is true of a return statement in a function. IE:

    PHP Code:
    function foo($var) {
       if( 
    $var === BAR ) {
          return 
    false;
       }
       return 
    true;

    Thank you for that, Derokorian. I've not done much more than modify other's functions, but will try to keep this method in use, as it seems much cleaner to me.

    thanks again!
    Warning: If you are taking advice from me, then you are in really bad shape. Let's both hope for your code's sake that someone comes along and pulls us out of this fix that I've gotten us into.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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