PHP嵌套的IF语句返回解析错误

时间:2016-01-28 20:17:51

标签: php if-statement

我有嵌套的IF语句来评估数量变量并相应地处理。我在php.net上检查了嵌套的IF语句,并遵循相同的规则,但仍然不起作用。

它解析了以下错误:

Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in

if(empty($quantity)){
    echo "<p>You did not enter <mark>Quantity</mark>.</p>";
    echo "<p>Please return to the form and re-enter the information <br><a href='conditionalsForm.html'>Go Back!</a>
</p>";
}
else {
    #ABS function for quantity and tax
    $quantity = abs($quantity);
    $tax = abs($tax) + 1;
    $totalCost = $quantity * $cost;

    if($quantity >= 5000){
        $totalCost = $totalCost - $discount;
    }

    #Calculate total cost to include tax.
    $totalCost = ($totalCost * $tax);
    $totalCost = round($totalCost);
    $monthlyPayment = ($totalCost / 12);

    #Nested IF to check the total cost for discount and output message
    elseif($quantity < 5000){
        echo "<p><mark>Discount will not apply as the sale is under $5,000.</p></mark>";    
    }
    endif;
}
endif;

1 个答案:

答案 0 :(得分:1)

您的语法错误,请查看我在下面所做的更新。

if(empty($quantity)){
    echo "<p>You did not enter <mark>Quantity</mark>.</p>";
    echo "<p>Please return to the form and re-enter the information <br><a href='conditionalsForm.html'>Go Back!</a>
</p>";
} else {
    #ABS function for quantity and tax
    $quantity = abs($quantity);
    $tax = abs($tax) + 1;
    $totalCost = $quantity * $cost;

    if($quantity >= 5000){
        $totalCost = $totalCost - $discount;
    } else {
        echo "<p><mark>Discount will not apply as the sale is under $5,000.</p></mark>";    
    }

    #Calculate total cost to include tax.
    $totalCost = ($totalCost * $tax);
    $totalCost = round($totalCost);
    $monthlyPayment = ($totalCost / 12);


}