我意识到我的代码可能像罪一样丑陋,但它一直在努力,直到这一刻,我才难倒。 If语句本身可以正常工作,但是当我在那里抛出For循环时却没有。
这是PHP:
if (!empty($cross)){ /* supposed to hide the empty form field */
$message .= "<tr><td width='185'><strong><em>Item<em></strong></td><td width='370'><strong><em>Type<em></strong></td><td width='185'><strong><strong><em>Qty<em></strong></strong></td></tr><tr><td width='185'><strong>Cross Braces</strong></td><td width='185'>";
for ($i = 0, $cross_number = count($cross), $j = 0, $crossqty_number = count($crossqty); $i < $cross_number, $j < $crossqty_number; $i++,$j++) {
$message .= "$cross[$i]</td><td width='185'>$crossqty[$j]</td></tr><tr><td width='185'></td><td width='370'>"; /* supposed to add a new row for the rest of the array */
}
$message .= "</td></tr>";
}
我终生难以理解为什么if语句不起作用。当表单字段留空时,它仍会显示在html电子邮件中。为什么这个if语句不能正常运行?
这是在扼杀我。字面意思是在这个表格上需要做的最后一件事。任何帮助都是极好的。谢谢。
答案 0 :(得分:1)
您无法在$cross[$i]
中使用""
- 返回
语法错误,意外T_ENCAPSED_AND_WHITESPACE,期待T_STRING 或T_VARIABLE或T_NUM_STRING
尝试:
<?php
if (!empty($cross)){ /* supposed to hide the empty form field */
$message .= "<tr><td width='185'><strong><em>Item<em></strong></td><td width='370'><strong><em>Type<em></strong></td><td width='185'><strong><strong><em>Qty<em></strong></strong></td></tr><tr><td width='185'><strong>Cross Braces</strong></td><td width='185'>";
for ($i = 0, $cross_number = count($cross), $j = 0, $crossqty_number = count($crossqty); $i < $cross_number, $j < $crossqty_number; $i++,$j++) {
$message .= $cross[$i] . "</td><td width='185'>" . $crossqty[$j] . "</td></tr><tr><td width='185'></td><td width='370'>";
}
$message .= "</td></tr>";
}
答案 1 :(得分:0)
您的$cross
数组是array ( 0 => '')
,这意味着两件事:
if
语句返回true
。for
循环至少会运行一次(如果$crossqty
非空也是如此)如果您的业务逻辑认为该数组实例为“空”,那么您可能应事先array_filter。