I have 24 days which have unique messages each day. How could i write the following IF statements in some sort of loop to make it more efficient: -
<?
if ($day == '1') {
$offerTxt = $day1Txt;
} else if ($day == '2') {
$offerTxt = $day2Txt;
} else if ($day == '3') {
$offerTxt = $day3Txt;
} else if ($day == '4') {
$offerTxt = $day4Txt;
} else if ($day == '5') {
$offerTxt = $day5Txt;
} else if ($day == '6') {
$offerTxt = $day6Txt;
}
?>
You can do this inline like this:
$offerTxt = ${'day'.$day.'Txt'};
You should probably check whether the day is in a certain set, so your code would come out looking similar to this:
$daysUsed = array(1,2,3,4,5,6);
$offerTxt = '';
if(in_array((int)$day, $daysUsed)) {
$offerTxt = ${'day'.$day.'Txt'};
}
You can use an array:
$array = $textforDays = array(
1 => 'Text for day 1',
2 => 'Text for day 2',
3 => 'Text for day 3',
4 => 'Text for day 4',
5 => 'Text for day 5',
6 => 'Text for day 6',
);
echo $textforDays[$day];