So I have a variable named bestmatchrule which returns a single number between 1-25 and now Im trying to assign that number a name using the following if statement:
$a=$bestmatchrule;
$b='';
if ($a == "1" || "2") {
$b="Chesil House";
} elseif ($a == "3" || "4") {
$b="Corfe House";
} elseif ($a == "5" || "6") {
$b="Cranborne House";
} elseif ($a == "7" || "8") {
$b="Dorchester House";
} elseif ($a == "9" || "10") {
$b="Lyme Regis House";
} elseif ($a == "11" || "12") {
$b="Okeford House";
} elseif ($a == "13" || "14") {
$b="Purbeck House";
} elseif ($a == "15" || "16") {
$b="Student Village";
} elseif ($a == "17" || "18" || "19") {
$b="Unilet";
} elseif ($a == "20") {
$b="Conel Court";
} elseif ($a == "21" || "22") {
$b="St John's Road";
} elseif ($a == "23" || "24" || "25") {
$b="Private Let";
} else {
"Please Try Again";
}
Problem is it doesn't work. It only returns the top one. Is this an issue with the value returned or the if statement?
Also, if there a way of shortening it to make it look nicer? I'm more focused on functionality, but there has to be a better way than this?
Thanks in advance! (p.s. sorry for the noob question!)
$a == "1" || "2"
is not a valid statement. It would need to be $a == "1" || "$a == 2"
. That's your error.
As far as shortening this, there isn't much you can do. You can use in_array()
to shorten some statements but you don't really have any where that would be truly handy.
if ($a == "1" || $a == "2") {
$b="Chesil House";
} elseif ($a == "3" || $a == "4") {
$b="Corfe House";
} elseif ($a == "5" || $a == "6") {
$b="Cranborne House";
} elseif ($a == "7" || $a == "8") {
$b="Dorchester House";
} elseif ($a == "9" || $a == "10") {
$b="Lyme Regis House";
} elseif ($a == "11" || $a == "12") {
$b="Okeford House";
} elseif ($a == "13" || $a == "14") {
$b="Purbeck House";
} elseif ($a == "15" || $a == "16") {
$b="Student Village";
} elseif (in_array($a, array("17", "18","19")) {
$b="Unilet";
} elseif ($a == "20") {
$b="Conel Court";
} elseif ($a == "21" || $a == "22") {
$b="St John's Road";
} elseif (in_array($a, array("23", "24", "25")) {
$b="Private Let";
} else {
echo "Please Try Again";
}
You can also use an array to hold your values wit the $a
value being the key. Then just check to see if it is in the array and if so, assign the value:
$array = [
1 => "Chesil House",
2 => "Chesil House",
3 => "Corfe House",
4 => "Corfe House",
5 => "Cranborne House",
6 => "Cranborne House",
7 => "Dorchester House",
8 => "Dorchester House",
9 => "Lyme Regis House",
10 => "Lyme Regis House",
11 => "Okeford House",
12 => "Okeford House",
13 => "Purbeck House",
14 => "Purbeck House",
15 => "Student Village",
16 => "Student Village",
17 => "Unilet",
18 => "Unilet",
19 => "Unilet",
20 => "Conel Court",
21 => "St John's Road",
22 => "St John's Road",
23 => "Private Let",
24 => "Private Let",
25 => "Private Let"
];
if (isset($array[$a])) {
$b = $array[$a];
}
else {
echo "Please Try Again";
}
John Conde's answer is probably what you want. One other possibility is progressive less than (<) statements.
$a= (int) $bestmatchrule;
$b='';
if ($a < 3) {
$b="Chesil House";
} elseif ($a < 5) {
$b="Corfe House";
} elseif ($a < 7) {
$b="Cranborne House";
} elseif ($a < 9) {
$b="Dorchester House";
} elseif ($a < 11) {
$b="Lyme Regis House";
} elseif ($a < 13) {
$b="Okeford House";
} elseif ($a < 15) {
$b="Purbeck House";
} elseif ($a < 17) {
$b="Student Village";
} elseif ($a < 20) {
$b="Unilet";
} elseif ($a < 21) {
$b="Conel Court";
} elseif ($a < 23) {
$b="St John's Road";
} elseif ($a < 26) {
$b="Private Let";
} else {
"Please Try Again";
}
A switch statement is also an option, but I don't think it would be much better.