if else语句为什么不运行else语句?

时间:2015-06-04 15:44:59

标签: php if-statement

我的PHP中的ifelse语句存在问题,它从不运行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;
}

2 个答案:

答案 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最有意义)......

  1. else if变为单独的if
  2. else if更改为else
  3. 或添加else子句
  4. 此外,正如@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; }
            }
        }
    }