0

给定一个数组,例如...

$array = array();
$array[0] = "blue" or "red" or "green" or "brown";

我将如何使用它来评估 if 语句并回显它?例如...

If ($ans1 = $array[0]) {
   echo "correct answer $array[0]";  // this should display "blue or red 
                                     //               or green or brown"
}
4

1 回答 1

2

您想使用另一个数组语句而不是or满足完全不同用例的运算符:

$array = array();
$array[0] = array("blue", "red", "green", "brown");

// new syntax
$array[0] = ["blue", "red", "green", "brown"];

然后,在您的输出代码中:

echo "Correct answers: " . implode(" or ", $array[0]);


顺便说一句,使用if ($ans1 = $array[0]) {正确!缺少一个等号:

if ($ans1 =  $array[0]) {
// --------^
if ($ans1 == $array[0]) {

`

于 2013-09-27T20:34:48.343 回答