每个PHP - 否则总是返回其他值

时间:2017-02-01 13:08:33

标签: php wordpress loops if-statement

我使用下面的代码发送报告,但是当我添加else语句时,即使if语句为真,也会返回所有语句。

谁能解释为什么这个?我以前不认为我曾经遇到过这个问题吗?

foreach ( $result as $page ) {
 $date1 = new DateTime($page->start_date);
 $date2 = new DateTime($page->end_date);

 if (strtotime($page->start_date) >= strtotime('today') && strtotime($page->start_date) < strtotime('tomorrow')) {

     $email_content .=  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>' . $page->post_title . ' </strong></span>';

     $email_content .=  '<span style=" font-size: 1em; font-family: arial, sans-serif; color:#00b200;"><strong>Order By '  . $date1->format('d-m-y') . ' ' . '</strong></span>';

     $email_content .=  '<div style="  font-size: 1em; font-family: arial, sans-serif; color:#cc0000;"><strong>For Delivery '  . $date2->format('d-m-y') . '</strong></div><br>' . '<br>';

 }else {
     $email_content =  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>tester </strong></span>';
 }
}    

感谢阅读:)

1 个答案:

答案 0 :(得分:1)

当你循环时,你的$ email_content变量会在else语句中被覆盖。你附加的if是。但在别的地方覆盖。尝试将代码更新为:

        foreach ( $result as $page ) {
     $date1 = new DateTime($page->start_date);
     $date2 = new DateTime($page->end_date);

     if (strtotime($page->start_date) >= strtotime('today') && strtotime($page->start_date) < strtotime('tomorrow')) {

         $email_content .=  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>' . $page->post_title . ' </strong></span>';

         $email_content .=  '<span style=" font-size: 1em; font-family: arial, sans-serif; color:#00b200;"><strong>Order By '  . $date1->format('d-m-y') . ' ' . '</strong></span>';

         $email_content .=  '<div style="  font-size: 1em; font-family: arial, sans-serif; color:#cc0000;"><strong>For Delivery '  . $date2->format('d-m-y') . '</strong></div><br>' . '<br>';
    }else {
         $email_content .=  '<span style=" background-colour: #777777; font-size: 1em; font-family: arial, sans-serif; color:#202020;"><strong>tester </strong></span>';

 }
}