我有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)
你可以这样内联这样做:
$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)
| 归档时间: |
|
| 查看次数: |
497 次 |
| 最近记录: |