具有比较运算符的回声数组索引 - PHP

时间:2013-09-27 20:31:31

标签: php arrays

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

$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"
}

1 个答案:

答案 0 :(得分: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]) {

`