如果条件不正常

时间:2012-11-01 11:56:53

标签: php if-statement

我在我的代码中实现了条件,条件是如果john设置比show abc.jpg else xyz.jpg但它不工作它每次都显示xyz .jpg图像。请帮我。如果我的语法错误,请告诉我错误在哪里。

<?php
if($extra = "John" || "Kevin" || "Cameron" || "Santosh" || "Ali"){ ?>
    <img src="http://yespricer.com/theme/Freshness/images/maria2.png" width="75"/>
<?php }else{ ?>
    <img src="http://yespricer.com/theme/Freshness/images/maria.png" width="75"/>
<?php } ?>

6 个答案:

答案 0 :(得分:3)

您需要每次检查$extra是否等于值。您的代码只执行一次。您可以将数组中的所有值分组,并检查您的值是否在其中:

$names = array("John", "Kevin", "Cameron", "Santosh", "Ali");
if(in_array($extra, $names)) {
    // $extra is in $names
}

答案 1 :(得分:0)

应该是

if($extra == "John" || $extra == "Kevin" || 
   $extra == "Cameron" || $extra == "Santosh" || 
   $extra == "Ali"){

}

修改

由于 des 建议in_array,您也可以使用此解决方案

$names = array("John", "Kevin", "Cameron", "Santosh", "Ali");

echo "<img src='http://yespricer.com/theme/Freshness/images/"
  .(in_array($extra, $names)?'maria1':'maria')
  .".png' width='75'/>";

答案 2 :(得分:0)

与上面的其他人提出的方式非常类似,但在我看来有点整洁

<?php
    $name = "";
    if($extra == "John" || $extra =="Kevin" || $extra =="Cameron" || $extra =="Santosh" || $extra == "Ali"){ 
        $name = "maria2.png";
    }else{
        $name = "maria.png";
    }
?>
<img src="http://yespricer.com/theme/Freshness/images/<?php echo $name;?>" width="75" />

答案 3 :(得分:0)

更好,更有效的方法是使用in_array()

<?php
if(in_array($extra, array("John", "Kevin", "Cameron", "Santosh", "Ali")){ ?>
    <img src="http://yespricer.com/theme/Freshness/images/maria2.png" width="75"/>
<?php }else{ ?>
    <img src="http://yespricer.com/theme/Freshness/images/maria.png" width="75"/>
<?php } ?>

答案 4 :(得分:0)

您无法使用

if($extra = "John" || "Kevin" || "Cameron" || "Santosh" || "Ali"){

原因

因为如果你从左到右,$ extra ==&#34; John&#34; ...好吧,那很好,因为根据$ extra的值,它可以评估为true或false。但是,下一个就是这样做:

if ("Kevin") {

由于非空字符串的计算结果为true,因此您可能已经写过:

if($extra = "John" || true || true || true || true){

由于您正在使用逻辑OR,因此每个$ extra都会导致if条件为TRUE。

解决方案1 ​​ - 凌乱的条件

if($extra == "John" || $extra == "Kevin" || $extra == "Cameron" 
|| $extra == "Santosh" || $extra == "Ali"){

解决方案2 - 如建议

$names = array("John", "Kevin", "Cameron", "Santosh", "Ali");
if(in_array($extra, $names)) {
    // $extra is in $names
}

解决方案3 - 比ifs更清洁,比阵列更快仍然灵活

switch ($extra) {
    case "John":
    case "Kevin":
    case "Cameron":
    case "Santosh":
    case "Ali":
        //code here for TRUE, if any of those names match $extra


        break;
    default:
        //code here for FALSE, if non of the names match $extra



}

答案 5 :(得分:0)

如何更多地了解正在发生的事情?将条件分配给变量:

$result = $extra = "John" || "Kevin" || "Cameron" || "Santosh" || "Ali";

然后让您的状况可见:

var_dump($result);

这会给你:

bool(true)

意味着条件导致布尔truedemo)。为什么会发生这种情况你现在可能会问自己。为此你可以分开选择条件:

"John" || "Kevin" || "Cameron" || "Santosh" || "Ali"

这是PHP中true的表达式。您可以使用true替换它。接下来的部分是:

$extra = true;

这很容易。您将true分配给变量$extra。现在是最后一部分:

$result = true;

再次,现在将表达式的结果分配给$result,以便看到true

相反,您可能想要检查变量$extra是否包含任何名称。 PHP中有一些注意事项可以正确地做到这一点。首先,让您的生活更轻松,并将您要检查的所有名称放入数组中:

$names = array("John", "Kevin", "Cameron", "Santosh", "Ali");

然后检查严格 $extra是否在该数组中:

$result = in_array($extra, $names, true);

注意这里的第三个参数是true,用于严格检查(参见in_arrayDocs)。否则,如果$extra 0 $result$image = $result ? 'maria2.png' : 'maria.png'; ,则可能会看到printf( '<img src="http://yespricer.com/theme/Freshness/images/%s" width="75"/>' , $image ); 。然后处理结果,但让你的生活再次轻松,你只需要一个变量:

$names  = array("John", "Kevin", "Cameron", "Santosh", "Ali");
$result = in_array($extra, $names, true);
$image  = $result ? 'maria2.png' : 'maria.png';
printf(
    '<img src="http://yespricer.com/theme/Freshness/images/%s" width="75"/>'
    , $image
);

然后最后做输出:

{{1}}

你已经解决了问题。完整的代码示例:

{{1}}

Demo

参见: