using php exit;

Discussion in 'PHP' started by mdrobiul, Aug 22, 2009.

  1. #1
    Do php experts and professionals strongly recommend of using exit in every step?

    Recently, i found such recommendation in user notes but never read any thing like that in my books.
     
    mdrobiul, Aug 22, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What step? You usually do exit() or die() after you do a header redirect, in case it doesn't work to stop from further execution. When certain errors occur, but otherwise, not much.
     
    premiumscripts, Aug 22, 2009 IP
  3. pepprs

    pepprs Peon

    Messages:
    195
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    exit() and die() are to make sure the execution stops for whatever reason. I dont use more than a few times.
     
    pepprs, Aug 22, 2009 IP
  4. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    don't actually need the backets. Well, if you have code that is normally executed, but shouldn't be that time around (because, for example, you are redirecting), use exit, by all means. It's not a professionalism thing, more a convenience thing. And you don't really need exit after redirecting if there is no code following
     
    Kyosys, Aug 22, 2009 IP
  5. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #5
    I prefer a good and fully functional script than a faster one.
    I dont know what proffesionals do.
    but every working script is a good one.
     
    astrazone, Aug 22, 2009 IP
  6. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yeah, don't need (). I use it if I don't want to 'else' the rest of the page. For example if you have a page that runs a bunch of queries and later you add in a section to check the queries, you can
    if($query >> badterms){echo "go away!";}else{//rest of script}

    or (what I do):

    if($query >> badterms){echo "go away!";exit;}


    You can also use it when you're checking multiple errors and an unaccounted error should exit the script. EG:

    if($error == "code 1"){echo "Code 1!";}
    elseif($error == "code 2"){echo "Code 2!";}
    else{echo "Not sure what happened. Panicking!";exit;}
     
    Goramba, Aug 22, 2009 IP
  7. Dollar

    Dollar Active Member

    Messages:
    2,598
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    90
    #7
    I recommend to use exit; when your debugging code and you need to check if your function is actually being invoked. Like

    if($var['something']) {
    echo "Were here";
    exit;
    //stuff
    }
    PHP:
    Also use exit; after when you use a header() since you will want to stop any further output.
    I do not recommend (and probably others) to use echo & exit; for displaying error messages to a website visitor. It is unprofessional. Better to make your own error function and have a way to show the error in the page somwhere.
     
    Dollar, Aug 22, 2009 IP
  8. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    's good for debugging though. Naturally, when you actually use it, you should use your template engine or whatever it is you do instead of just showing a white screen with one sentence.
     
    Kyosys, Aug 23, 2009 IP
  9. ipr22

    ipr22 Peon

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Good for debugging but make sure you don't leave it in the wrong places by accident. Non-execution of code later on could potentially mess you up.
     
    ipr22, Aug 23, 2009 IP
  10. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Not the case at all. You can't account for every possible error, especially in a complex script. The exit example I posted should always be included or else you could have a runaway script.
     
    Goramba, Aug 23, 2009 IP
  11. Dollar

    Dollar Active Member

    Messages:
    2,598
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    90
    #11
    Okay except for the most obscure error you might place echo "shouldn't happen" and exit;. I meant in parts of your script that handle say.. a registration form.
    You wouldn't want to be using echo and exit to tell them their username is already taken or their email is invalid etc. That would be really lame.
     
    Dollar, Aug 24, 2009 IP
  12. bulkemailpp

    bulkemailpp Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    The exit() function at the end of your scripts is a good way how to prevent automatic bot hacks, because bots add their malicious code at the end of the scripts like index.php, main.php etc. The exit() function is a good counteraction.
     
    bulkemailpp, Aug 24, 2009 IP
  13. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Err, how about not get hacked in the first place? :) This is the first time I hear that as a suggestion.
     
    premiumscripts, Aug 24, 2009 IP
  14. AdnanAhsan

    AdnanAhsan Well-Known Member

    Messages:
    601
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #14
    mate it depends upong your coding steps. If you are not going to redirect you can even use return statement. your coding style should be wel formed so you can understand easily whats going on .
     
    AdnanAhsan, Aug 24, 2009 IP