what's wrong with this statement?

Discussion in 'PHP' started by vassili, Mar 7, 2008.

  1. #1
    $stock_idWidth = ($stock_ID == ("7" || "C" || "5")  ? "139" : "250"); 
    Code (markup):
    this will give me a false everytime even when $stock_ID != the 3 values... :confused:
     
    vassili, Mar 7, 2008 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,901
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    It needs to look like this

    $stock_idWidth = ($stock_ID == ('7' || 'C' || '5')) ? '139' : '250';
    Code (markup):
    Your bracket was in the wrong place :)
     
    sarahk, Mar 7, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    You cannot compare values like that.

    Do it this way instead:
    
    $stock_idWidth = in_array($stock_ID, array(7, "C", 5))  ? 139 : 250;
    
    PHP:
     
    nico_swd, Mar 8, 2008 IP
    sarahk likes this.
  4. lephron

    lephron Active Member

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #4
    The || operator doesn't work like that. Nico_swd has provided a working example that doesn't use ||, but if you wanted to your code would need to be:

    
    $stock_idWidth = (($stock_ID == "7" || $stock_ID ==  "C" || $stock_ID == "5")  ? "139" : "250");
    
    Code (markup):
     
    lephron, Mar 8, 2008 IP
  5. vassili

    vassili Peon

    Messages:
    36
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    cool, gonna try this.. thanks guys
     
    vassili, Mar 9, 2008 IP