1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need help with IF statements

Discussion in 'PHP' started by ipunkbali, Dec 18, 2016.

  1. #1
    I have urls like this
    index.php?k1=kilometers&k2=feet&q=4
    index.php?k1=miles&k2=inch&q=4

    i want to set values for $k1Abbreviation and $k2Abbreviation

    $q = $_GET["q"];
    $k1 = $_GET["k1"];
    $k2 = $_GET["k2"];
    
    if ($k1 = "kilometers" OR $k2 = "kilometers"){
    $k1Abbreviation = "in";
    $k2Abbreviation= "in";
    }
    elseif ($k1 = "feet" OR $k2 = "feet"){
    $k1abbr = "ft";
    $k2abbr = "ft";
    }
    elseif ($k1 = "miles" OR $k2 = "miles"){
    $k1abbr = "mi";
    $k2abbr = "mi";
    }
    
    echo $k1 . $k1Abbreviation .'to'. $k2 . $k1Abbreviation
    PHP:
    the above code isnt working right.
    my goal is to set value for the abbreviation and display something like "kilometers (km) to feet (ft)"
     
    Last edited by a moderator: Dec 18, 2016
    ipunkbali, Dec 18, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    You're not comparing, you're assigning. = is assigning, == is comparing.

    Do this:
    
    if ($k1 == 'kilometers' || $k2 == 'kilometers'){
    $k1Abbreviation = 'in';
    $k2Abbreviation= 'in';
    } elseif ($k1 == 'feet' || $k2 == 'feet'){
    $k1abbr = 'ft';
    $k2abbr = 'ft';
    } elseif ($k1 == 'miles' || $k2 == 'miles'){
    $k1abbr = 'mi';
    $k2abbr = 'mi';
    }
    
    PHP:
     
    PoPSiCLe, Dec 18, 2016 IP
  3. ipunkbali

    ipunkbali Well-Known Member

    Messages:
    276
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #3
    Still having the same problem,

    echo $k1 . $k1Abbreviation .'to'. $k2 . $k1Abbreviation

    wrong output: "kilometers (km) to feet (km)", it should be "kilometers (km) to feet (ft)"

    i dont know whats wrong
     
    ipunkbali, Dec 18, 2016 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    There's some logic that's inherently wrong - @PoPSiCLe fixed up the syntax but I'm going to suggest an alternate approach

    $abbr = array('km' => 'kilometres', 'ft' => 'feet', 'mi' => 'miles');
    
    $k1Abbreviation = array_search($k1, $abbr);
    $k2Abbreviation = array_search($k2, $abbr);
    
    echo "{$k1} ({$k1Abbreviation}) to {$k2} ({$k1Abbreviation})";
    PHP:
     
    sarahk, Dec 18, 2016 IP
  5. ipunkbali

    ipunkbali Well-Known Member

    Messages:
    276
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #5
    Wow.. thank you.. it works. :)
     
    ipunkbali, Dec 18, 2016 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #6
    The trick is to understand why it works so that when presented with a similar problem in the future you can use it again.
     
    sarahk, Dec 18, 2016 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    What's wrong is that you're using the same variable twice, expecting different content / results...
     
    PoPSiCLe, Dec 18, 2016 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #8
    lol, I didn't see that. I was so hung up on the if then else.
     
    sarahk, Dec 19, 2016 IP