问题描述:

I'm creating a verb conjugator based on the endings of a verb that the user inputs.

For now it should work on verbs ending with "aic", "eic", "oic" (one array of endings) and "bic", "fic", "mic", "nic", "pic" and "wic" (another array of endings).

Unfortunately, it keeps assigning the first array to anything I put - even if it doesn't end with the specified endings.

This is the page: www.alweso.2ap.pl/koniugator/index.php

Here's the code:

<?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>

网友答案:

Who told you that would work? Where did you read that? That isn't in any documentation; it doesn't work like that.

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

// ...

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

Alternatively:

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
}
网友答案:

You can't do if($value = ($a || $b || $c)) do if(in_array($value, array($a, $b, $c)). if($value = ($a || $b || $c)) means : $value == true, because 'foo' || 'bar' || 'barfoo' is evaluate as true (non-empty strings are alway true in php).

网友答案:

your if statement syntax is incorrect, you cant do if($last3 == (x || y || z)) because its illogical, this statement reads `if ($last3 == (x == true || y == true || z == true))

you must write it out fully:

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

or use in_array()

相关阅读:
Top