I need some help with Logical Operators

Discussion in 'PHP' started by filoaman, Jan 10, 2012.

  1. #1
    Hi
    Here is my question:
    I need to use a combination of conditions in order to execute the function i want.
    I want something like this:

    if (value1 <= x && value1 >= y) OR (value1 <= z && value1 >= n);
    doTheFunction;
    } else {
    dontDoTheFunction
    }

    What is the right way to do this?

    Thanks in advance
     
    filoaman, Jan 10, 2012 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    I'd write it slightly different:
    
    if ((value1 <= x) && (value1 >= y)) || ((value1 <= z) && (value1 >= n));
    doTheFunction;
    } else {
    dontDoTheFunction
    }
    
    Code (markup):
    The extra parens are because I can never remember the rules of presidence and I use the "||" operator by habit because I use several different languages.
     
    rainborick, Jan 10, 2012 IP
  3. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #3
    The extra parens are for stating to perform this first then move onto the other code.
     
    HuggyEssex, Jan 10, 2012 IP
  4. filoaman

    filoaman Greenhorn

    Messages:
    44
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    Finally it works this way

    if ((value1 <= x && value1 >= y) || (value1 <= z && value1 >= n)){
    doTheFunction;
    } else {
    dontDoTheFunction;
    }
    PHP:
    The script need parenthesis but only two sets in oder to group the two conditions.
    And i had also forget to put the "{" symbol at the end of the first line. I use ";" instead and this was wrong.

    Thank you for your help
     
    filoaman, Jan 10, 2012 IP
  5. modz

    modz Well-Known Member

    Messages:
    95
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    103
    #5
    modz, Jan 11, 2012 IP
  6. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #6
    actually your in the right way.

    occasionally when i do "or" statement specially 2 or more conditions i do it like this

    <code>
    if condition1 {
    do method;
    return;
    }

    if condition2 {
    do method;
    return;

    ....
    </code>
    if the first conditions is satisfied automatically execute the method the cut the process
    in that way i can instantly eliminating the process of reading the following conditions.
     
    bartolay13, Jan 11, 2012 IP