OR command with NOT EQUALTO help

Discussion in 'PHP' started by ian_ok, Nov 4, 2005.

  1. #1
    If a user enters in (YES/NO/Don't know) I want code to continue testing if they enter in anything else I want the error message to appear.

    My code below isn't working:

    elseif ($hosting != "Yes" || $hosting != "No" || $hosting != "Don't know")
    {
    echo "Error.......";
    }
    PHP:
    Can anyone advise.

    Thanks Ian
     
    ian_ok, Nov 4, 2005 IP
  2. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should then use AND instead of OR. This way if Any of the 3 tests fails, meaning YES/NO/DON'T KNOW is found, it will not fall into the error. Like this:

    
    elseif ($hosting != "Yes" && $hosting != "No" && $hosting != "Don't know")
    {
        echo "Error.......";
    }
    
    PHP:
     
    durango, Nov 4, 2005 IP
    ian_ok likes this.
  3. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks works a treat, i also did the following more complicated route which worked....

    function validate_hosting($a) {
    $forbidden = array('Yes', 'No', 'Do not know');
    foreach ($forbidden as $b)
    if (strpos($a, $b) !== false) return true;
     return false;
    }
    
    elseif (!validate_hosting($_POST['hosting'])) {
    echo "Sorry you are trying to alter the'Do you require hosting field' which you have no access to";
    }
    PHP:
    Ian
     
    ian_ok, Nov 4, 2005 IP
  4. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you're using a select box for "Yes","No", and "Do not know", you could simply do this then:

    
    $forbidden = array('Yes', 'No', 'Do not know');
    function validate_hosting($a) {
        global $forbidden;
        return in_array($a,$forbidden);
    }
    
    PHP:
    No messy foreach loop :)
     
    durango, Nov 5, 2005 IP
  5. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for that, yes do have combo drop down and want to make sure I don't get hit by bots, hence going OTT in checking everything.

    Ian
     
    ian_ok, Nov 6, 2005 IP