SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    die() or echo to handle if statement

    Hi. I have been working on a survey all works great. Just wondering if I'm handing this the right way or not. After a code is entered and submitted I run it through a couple of checks. First is the code proper length (10 digits), is the code in the database and has this code been used. If the code passes all of these. I then start session and the survey can be completed. sample code below.
    PHP Code:
    if($rowCheck >= "1") {
             
    $row mysql_fetch_array($query);
            
    $status $row['status'];
            
    $washcode $row['washCode'];
            }
            if(
    $status == "1"){
            die(
    '<div align="center"><h1>Error the code you have entered has already been used.</h1><form action="'$_SERVER['PHP_SELF'].'" method="POST">
        <tr>
            <th colspan="2">Please check your code and try again.<br />
         <input type="text" name="code" autocomplete="off"><br />
        <input type="submit" name="submitcode" value="Submit Code"></th>
        </tr>
    </form></div>'
    );
            
            }
            else {
            
    session_start();
            
    $_SESSION['code'] = $code;
            
    $_SESSION['redeemCode'] = $washcode;
            echo 
    '<div align="center"><b>Thank you for taking the time to complete our survey. Please click the link below to begin.<br /><a href="visitdate.php"><button type="button">Next</button></a></b></div>';
            } 
    My main concern is that if the code does not pass this check I am currently using die(), and the rest of the page is not loaded leaving off the html footer and side nav. I am wanting to switch to echo''; to handle the error. Would this be alright? And can I get a little info on when it is best to use die(); instead of echo'';

    I am working on converting all of my sql to mysqli when I started thinking about this.

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For me, die() should be a rare sight in production code. It should be used only when the error is unrecoverable. Even then, for most cases you'll want to have a nice message with a full page, etc.
    - Robert

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2011
    Location
    Colorado, USA
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by QMonkey View Post
    For me, die() should be a rare sight in production code. It should be used only when the error is unrecoverable. Even then, for most cases you'll want to have a nice message with a full page, etc.
    Thanks QMonkey. That's what I started thinking when I started going through the code tonight. Not sure why I was using die(). But I'll be changing them to echo tomorrow. Thanks again for your reply.

  4. #4
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    how about throwing exceptions?


Bookmarks

Posting Permissions

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