Hi,
RE: shortcut to multiple if-else statements?
I have a small question which I was hoping that someone might have an answer for:
I have 4 variables: $item1, $item2, $item3 and $item4. Let's say for example that only 3 of these 4 variables have a value:
$item1="shoes";
$item2="jacket";
$item4="waistcoat";
The question is: "Is there a way to write a loop such that it will print the variable's values if it is not Null?"...something like:
for ($i=1; $i<5; $i++) {
if (isset('$item'$i)){
echo "Item $i is a".( '$item'.$i);
}
}
I know the above code is a load of gibberish and doesn't work...but I was wondering if there is a way to generate the strings $item1, $item2, $item3, item4 .in the for loop within isset() and echo?
Currently, I am writing separate if-else statements for each variable, that is:
if (isset($item1)){
echo ""Item 1 is a $item1.";
}
if (isset($item2)){
echo ""Item 2 is a $item2.";
}
.....and so on. This is obviously a little tedious if you have more than 4 items!!
Can anyone think of a shortcut.....short of putting everything in an array?
Thanks in advance for any suggestions given.
Allen.