另一个PHP if / else没有按预期工作

时间:2012-10-05 16:03:36

标签: php

我无法找到这个问题的解决方案,并希望另一组眼睛可以接受某些东西。当第一个“if”不为真时我没有执行else $ confirm =行,我无法弄清楚原因。

if($_POST['zip'] && $_POST['country']=='USA' && !empty($fax)){
    $plus4 = substr($_POST["zip"],6,4);
    if (empty($plus4)) {
        $link = '<a href="http://zip4.usps.com/zip4/welcome.jsp?city='.$city.'&address2='.$address.'&state='.$state.'&zip5='.$zip.'" class="greybox" onclick="window.scrollTo(0,0)">Zip + 4</a>';
        $msg = "Your Zip + 4 was not provided! We cannot fax your Representative without your Zip+4.<br/>Click here to find your ".$link ;
        print "<p style=\"color: red;\"><b>$msg<br/><br/></b></p>";
    }
    if (empty($_POST['state']) || empty($_POST['zip'])) $statezip = false ; // Check for State & Zip Required
    else $statezip = true ;
    //if (empty($state) || empty($zip) || empty($plus4)) $statezip = false ; // Check for State & Zip Required
    $confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($_POST['address']) && !empty($_POST['city']) && !empty($_POST['country']) && $statezip == true ; // Verify required fields
}
else $confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields

3 个答案:

答案 0 :(得分:1)

由于最后一个条件,您的else语句将始终返回false $confirm

&& $statezip == true

您正在if语句中设置该值,因此else语句中未定义该值。

答案 1 :(得分:0)

将花括号添加到ELSE语句中。由于您在定义IF中使用它们,因此解析器期望它们。

else {
$confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields
}

答案 2 :(得分:0)

使用isset检查变量是否存在,
function empty返回true,即使它包含0
当你使用else时,也使用方括号{ else{do something}