PHP - IF不检查条件是否为真

时间:2014-04-01 13:06:01

标签: php if-statement

我根据用户输入的动词的结尾创建动词共轭器。

现在它应该适用以" aic"," eic"," oic"结尾的动词。 (一系列结局)和" bic"," fic"," mic"," nic"," pic&#34 ;和" wic" (另一组结局)。

不幸的是,它一直将第一个数组分配给我所放的任何东西 - 即使它没有以指定的结尾结束。

这是页面:www.alweso.2ap.pl/koniugator/index.php

以下是代码:

<?php

$verb = $_POST["verb"];
$last2 = substr ($verb, -2);
$last3 = substr($verb, -3);
$last4 = substr($verb, -4);
$last5 = substr($verb, -5);
$last6 = substr($verb, -6);
$last7 = substr($verb, -7);
$last8 = substr($verb, -8);
$last9 = substr($verb, -9);

$root2 = str_replace($last2, "", $verb);
$root3 = str_replace($last3, "", $verb);
$root4 = str_replace($last4, "", $verb);
$root5 = str_replace($last5, "", $verb);
$root6 = str_replace($last6, "", $verb);
$root7 = str_replace($last7, "", $verb);
$root8 = str_replace($last8, "", $verb);
$root9 = str_replace($last9, "", $verb);


$aic_eic_oic = array("je", "isz", "i", "imy", "icie", "ja" );   
$bic_fic_mic_nic_pic_wic = array("ie", "isz", "i", "imy", "icie", "ia" );   
$cic_lic_sic_zic = array("e", "isz", "i", "imy", "icie", "a" ); 

?>
    <div class="container">
      <div class="row">
      <div class="col-md-4 col-md-offset-4">

<?php

if ($last3 == ("aic" || "eic" || "oic")) {
        foreach ($aic_eic_oic as $one) {
        echo "<li>$root2$one</li>";
    }   
    }

elseif ($last3 == ("bic" || "fic" || "mic" || "nic" || "pic" || "wic")) {
        foreach ($bic_fic_mic_nic_pic_wic as $one) {
        echo "<li>$root2$one</li>";
        }
    }

    ?>

</div>
</div>
</div>

3 个答案:

答案 0 :(得分:5)

你不能if($value = ($a || $b || $c))if(in_array($value, array($a, $b, $c))if($value = ($a || $b || $c))表示:$value == true,因为'foo' || 'bar' || 'barfoo'的评估为真(非空字符串在php中总是如此)。

答案 1 :(得分:1)

谁告诉你这样会有用?你在哪里读到的?这不是任何文件;它不会那样工作。

if( in_array($last3, array("aic","eic","oic")))

// ...

elseif( in_array($last3, array("bic","fic","mic","nic","pic","wic")))

可替换地:

switch($last3) {
  case "aic":
  case "eic":
  case "oic":
    // do something
    break;
  case "bic":
  case "fic":
  case "mic":
  case "nic":
  case "pic":
  case "wic":
    // do something else
    break;
  default:
    // optional, do something to indicate error
}

答案 2 :(得分:0)

你的if语句语法不正确,你不能if($last3 == (x || y || z)),因为它不合逻辑,这句话读作`if($ last3 ==(x == true || y == true || z == true) )

你必须完全写出来:

if ($last3 == x || $last3 == y || $last3 ==z)

或使用in_array()