我的PHP中的if
和else
语句存在问题,它从不运行else
语句。
输入表单是HTML格式:
<input type="radio" name="marital_stat" id="single" value="single" />Single
<input type="radio" name="marital_stat" id="married" value="married" />Married
<input name="age" type="text" size="5" maxlength="3" placeholder="30" required/>
<input name="work" type="radio" id="employee" value="employee" />Employee
<input name="work" type="radio" id="own" value="own" />
Own Business
<input name="work" type="radio" id="jobless" value="jobless" />Jobless
<input name="place" type="radio" id="urban" value="urban" />Urban
<input name="place" type="radio" id="rural" value="rural" />Rural</td>
这是PHP代码:
if ($marital_stat == 'married')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'jobless')
{
if ($place == 'rural') { $loan_credibility == 5; }
}
}
}
else if ($marital_stat == 'single')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'employee')
{
if ($place == 'rural') { $loan_credibility == 1; }
}
}
}
这是一个显示一些输出的条件:
$A = 'positive';
$B = 'negative';
if ($loan_credibility == 5 ){
echo $B ;}
else{
echo $A;
}
答案 0 :(得分:2)
我看到你做$loan_credibility == 5;
或1
; ==
仅在等式语句中使用它检查双方是否相等,您必须使用=
来设置值,而不是==
所以它将是$loan_credibility = 5;
或{ {1}};
答案 1 :(得分:0)
您没有else
子句(使用else if
时在逻辑上是必要的)...
if (condition) {
code to be executed if condition is true;
} elseif (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
执行以下任何操作(#1或#2最有意义)......
else if
变为单独的if
块else if
更改为else
else
子句此外,正如@Mohamed Belal所指出的,设置变量时使用=
而不是==
。
解决问题的两件事:1)if-else if-else
逻辑和2)=
vs ==
......
if ($marital_stat == 'married')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'jobless')
{
if ($place == 'rural') { $loan_credibility = 5; }
}
}
}
if ($marital_stat == 'single')
{
if ($age >= 18 || $age < 59)
{
if ($work == 'employee')
{
if ($place == 'rural') { $loan_credibility = 1; }
}
}
}