What is wrong with this function

Discussion in 'PHP' started by CuBz, Jul 9, 2009.

  1. #1
    Can someone tell me what is wrong here please:

    if ($gender != 'Male' OR 'Female' OR 'Unknown'){
    die("That Dosn't Work Here Smart Ass HAHA");
    }elseif ($gender = 'Male' OR 'Female' OR 'Unknown'){
    PHP:
    Thanks
     
    CuBz, Jul 9, 2009 IP
  2. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi,

    Just two simple problems with your use of operators

    in PHP you do not use OR (in SQL you do), instead you use || to represent this.

    Also = is an assignment operator, for example when you use $gender = "male", you are assigning the String male to the variable gender. Inside your if statement you need to use a comparison operator, which is an equals sign twice (==).

    For more on operators, see here
    it's a really basic and quick lowdown on arithimatic logical and comparison operators in php.

    Anyway based on what i've said above it would make your statement:
    
    if ($gender != 'Male' || 'Female' || 'Unknown'){
    die("That Dosn't Work Here Smart Ass HAHA");
    }
    elseif ($gender == 'Male' || 'Female' || 'Unknown'){
    //Blah
    }
    PHP:
     
    wd_2k6, Jul 9, 2009 IP
  3. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #3
    A logical mistake is here. I guess, the operator should be AND not OR for if().

    The if() checks if the gender is neither of Male, Female or Unknown and elseif() checks if the gender is either of these there. So && for if() and || for elseif() will be appropriate.
     
    techbongo, Jul 9, 2009 IP
  4. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #4
    This doesnt work on PHP.

    The correct syntax is:
    
    if ($gender != 'Male' || $gender !=  'Female' ||$gender !=  'Unknown'){
    die("That Dosn't Work Here Smart Ass HAHA");
    }
    else{
    //Blah
    }
    
    PHP:
    Also, for this particular example, the conditions placed on the elseif is redundant.
     
    samyak, Jul 9, 2009 IP
  5. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, my bad thought that might be the case
     
    wd_2k6, Jul 10, 2009 IP