PHP if / else语句问题

时间:2012-05-16 01:24:00

标签: php if-statement

我对此声明有一些问题,

<?php 
    $cert_mess = $vehicle['make'];
    if ($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0")  {
        $cert_mess = "DFWCertAutos";
    }
    elseif ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0") {
        $cert_mess = "DFWCertAutos";
    }
    elseif (!in_array($vehicle['make'], array('Cadillac','Honda') )) {
        $cert_mess = "DFWCertAutos";
    }
?>
<div style="font-size:10px; padding:10px; padding-top: 0px;">*This car is <?php 
echo $cert_mess ?> certified.</div>

有什么建议吗?目前它只显示$cert_mess为'make'并忽略if / else if语句。

2 个答案:

答案 0 :(得分:1)

更简单的代码如下:

$cert_mess = $vehicle['make'];
if (($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0")
    || ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0")
    || !in_array($vehicle['make'], array('Cadillac','Honda'))
) {
    $cert_mess = "DFWCertAutos";
}

答案 1 :(得分:1)

更简单:

$cert_mess = $vehicle['make'];
if (!in_array($vehicle['make'], array('Cadillac', 'Honda')) || $vehicle['certified'] == '0')
{
    $cert_mess = 'DFWCertAutos';
}