我正在根据用户输入的动词的结尾创建一个动词变位器。
现在,它应该处理以"aic","eic","oic"(一组结尾)和"bic","fic","mic","nic","pic"和"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>
你不能做if($value = ($a || $b || $c))
做if(in_array($value, array($a, $b, $c))
。 if($value = ($a || $b || $c))
的意思是:$value == true
,因为'foo' || 'bar' || 'barfoo'
被评估为真(非空字符串在php中总是为真)。
谁告诉你会起作用?你在哪里读到的?这在任何文档中都没有;它不是那样工作的。
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
}
你的if语句语法不正确,你不能做if($last3 == (x || y || z))
因为它不合逻辑,这个语句读'if ($last 3 == (x == true || y == true || z == true))
你必须把它完整地写出来:
if ($last3 == x || $last3 == y || $last3 ==z)
或使用in_array()