SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,760
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Positive, then Negative (or the other way around)?

    When you have an IF-THEN-ELSE, do you start with the "positive" portion first, and then save the "negative" for last, or visa-versa?

    For example, Positive first...
    PHP Code:
        if (isset($_GET['x']) && strlen($_GET['x'])==40){
            
    // Valid Format.
            
    $tempPassword $_GET['x'];
        }else{
            
    // Invalid Format.
            
    $_SESSION['resultsCode'] = 'PASSWORD_INVALID_TEMP_PASSWORD';

            
    // Redirect to Outcome Page.
            
    header("Location: " BASE_URL "members/change_password_results.php");

            
    // End script.
            
    exit();
        }
    //End of CHECK FOR TEMP PASSWORD 

    For example, Negative first...
    PHP Code:
        if (!isset($_GET['x']) && strlen($_GET['x'])!==40){
            
    // Invalid Format.
            
    $_SESSION['resultsCode'] = 'PASSWORD_INVALID_TEMP_PASSWORD';

            
    // Redirect to Outcome Page.
            
    header("Location: " BASE_URL "members/change_password_results.php");

            
    // End script.
            
    exit();
        }else{
            
    // Valid Format.
            
    $tempPassword $_GET['x'];
        }
    //End of CHECK FOR TEMP PASSWORD 

    Debbie

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Maybe off topic, but still:
    this
    PHP Code:
     if (!isset($_GET['x']) && strlen($_GET['x'])!==40){ 
    is not the negation of
    PHP Code:
    if (isset($_GET['x']) && strlen($_GET['x'])==40){ 
    This is the negation:
    PHP Code:
     if (!isset($_GET['x']) || strlen($_GET['x'])!==40){ 
    So, even though IMO it makes no difference if you put the positive first or not, it might be easier, especially for if's with composite conditions.

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Basically, when performance matters, you want the IF statement to be the one that's most frequent, but in PHP - performance of that kind of level is irrelevant, therefore you go with the one that is easiest to understand, which for most people is not negation (it would of been easier to understand if I said "which for most people is the positive first" - hopefully that illustrates my point).

    So to answer your question, I would say the first is the better version, assuming your second one was fixed to be a correct negation.


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
  •