另一个PHP if/else未按预期工作
我还没能找到解决这个问题的办法,希望另一批人能注意到一些事情。当第一个“if”不为真时,else$confirm=行不执行,我不知道为什么另一个PHP if/else未按预期工作,php,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://
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
if($\u POST['zip']&&$\u POST['country']=='USA'&&&!empty($fax)){
$plus4=substr($_POST[“zip”],6,4);
如果(空($plus4)){
$link='';
$msg=“未提供您的Zip+4!没有您的Zip+4,我们无法向您的代表发送传真。
单击此处查找您的“..$link;
打印“$msg
”;
}
if(empty($_POST['state'])| | empty($_POST['zip']))$statezip=false;//检查state&zip是否需要
else$statezip=true;
//如果(empty($state)| | empty($zip)| | empty($plus4))$statezip=false;//检查是否需要状态和zip
$confirm=!empty($\u POST['name'])和&!empty($\u POST['from'])和&!empty($\u POST['address'])和&!empty($\u POST['country'])和&$statezip==true;//验证必填字段
}
否则$confirm=!空($_POST['name'])&&!空($_POST['from'])&&!空($地址)&!空($city)&!空($country)&&$statezip==true;//验证必填字段
在ELSE语句中添加花括号。因为您在定义IF时使用了它们,所以解析器需要它们
else {
$confirm = !empty($_POST['name']) && !empty($_POST['from']) && !empty($address) && !empty($city) && !empty($country) && $statezip == true ; // Verify required fields
}
由于上一个条件,对于
$confirm
,您的else
语句将始终返回false
:
&& $statezip == true
您正在
if
语句中设置该值,因此在else
语句中未定义该值。使用isset检查变量是否存在,函数empty返回true,即使它包含
0
,使用else时,也要使用括号
{
else{do something}
您已经通过var\u dump()
检查了$\u POST['zip']、$\u POST['country']、$fax
的内容。这有点乱,需要正确地分离if语句。您访问$address
而不是$\u POST['address']有什么区别吗
。为了确保else条件按您的意愿运行,您应该将代码放在花括号内,例如,else{…}
,与您使用它时的操作相同。这些条件中有没有应该是elseif的??