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)"
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:
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
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:
The trick is to understand why it works so that when presented with a similar problem in the future you can use it again.