【问题标题】:Combining for-loops and if statements in PHP在 PHP 中结合 for 循环和 if 语句
【发布时间】:2017-08-06 06:39:34
【问题描述】:
for($whichPages = 0, $whichPages < $arrlength, $whichPages++) {if(($includeHome=='YES') && $today > $alertbegin && $today < $alertend) echo '<div class="msg" style="margin-bottom:25px">'.$alert.'</div>'};

上面的代码有一些语法问题。基本上我想说如果在数组 $whichPages 中,如果其他条件为真,则回显 html。不确定如何组合 for 循环和 if 子句。帮助不大。谢谢。

我有这段代码可以工作,但我希望对其进行简化

if(($includeHome=='YES' || $thisPage=='Class' || $thisPage=='Events' || $thisPage=='Gallery') && $today > $alertbegin && $today < $alertend)echo '<div class="msg" style="margin-bottom:25px">'.$alert.'</div>';

$thisPage 变量是我放入数组 $whichPages = array('Class','Events','Gallery')

的内容

【问题讨论】:

    标签: php if-statement for-loop


    【解决方案1】:

    使用以下方法:

    $whichPages = ['Class','Events','Gallery'];
    if ((in_array($thisPage, $whichPages) || $includeHome == 'YES') 
        && $today > $alertbegin && $today < $alertend) {
        echo '<div class="msg" style="margin-bottom:25px">' . $alert . '</div>';
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 2011-10-22
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-05
      相关资源
      最近更新 更多