PHP循环显示每天的唯一消息

Ame*_*sey 2 php loops

我有24天每天都有独特的信息.我怎样才能在某种循环中编写以下IF语句以提高效率: -

<?

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;

} 


?>
Run Code Online (Sandbox Code Playgroud)

Ric*_*rds 6

你可以这样内联这样做:

$offerTxt = ${'day'.$day.'Txt'};
Run Code Online (Sandbox Code Playgroud)

您应该检查一天是否在某一组中,因此您的代码看起来与此类似:

$daysUsed = array(1,2,3,4,5,6);
$offerTxt = '';
if(in_array((int)$day, $daysUsed)) {
    $offerTxt = ${'day'.$day.'Txt'};
}
Run Code Online (Sandbox Code Playgroud)