How do I simplifiy: if 1 then var = yes else var = 2

Discussion in 'PHP' started by bobby9101, Feb 11, 2007.

  1. #1
    I am trying to pull out of the deep corners of my brain how to do:
    
    if ($var = 1) {
    $secondvar = hi;
    }
    elseif ($var = 2) {
    $secondvar = bye;
    }
    
    PHP:
    in a single line, I thought i remember some shorthand way of doing it. (possibly using ? or : in the code)
     
    bobby9101, Feb 11, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    $secondvar = $var == 1 ? 'hi' : 'bye';
    
    
    PHP:
     
    nico_swd, Feb 11, 2007 IP
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ahhh thanks a million...
     
    bobby9101, Feb 11, 2007 IP
  4. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #4
    is it possible to add a third part?
    so simplifying:
    
    if ($var == 1) {
    $secondvar = hi;
    }
    elseif ($var == 2) {
    $secondvar = bye;
    }
    elseif ($var == 3) {
    $secondvar = good luck;
    }
    
    Code (markup):
     
    bobby9101, Feb 11, 2007 IP
  5. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I don't think you can do three, it's straight boolean I'm pretty sure. Any real reason you want to do it like that though? A lot of coders have never seen that before and most people just do:
    
    if ($var)
      echo 'blah';
    else
      echo 'blah2';
    
    PHP:
    It's just a bit easier to read in the long run I think.
     
    projectshifter, Feb 11, 2007 IP
  6. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Dude, you need to balance between being lazy and keeping the code readable :)
    But yes, you could do that if you want.

    $secondvar = ($var == 1) ? "hi" : (($var == 2) ? "bye" : "good luck");

    Strictly speaking it needs another check for ($var == 3) because your last statement was an 'elseif' not plain 'else'.
     
    phper, Feb 11, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Couldn't agree more. I hate messy code.
     
    nico_swd, Feb 12, 2007 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    IMO I'd use a switch struture.
     
    krakjoe, Feb 12, 2007 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Or even arrays.

    
    
    $foo = array(
        1 => 'hi',
        2 => 'bye',
        3 => 'good luck'
    );
    
    $secondvar = isset($foo[$var]) ? $foo[$var] : 'default';
    
    
    PHP:
     
    nico_swd, Feb 12, 2007 IP