在php中编写已经创建的if-else语句的switch语句


writng switch statement of an already made if-else statement in php

我刚刚开始使用php,我想知道我们如何编写switch等价于以下if..else语句:

$op1 = array("12", "13", "14");
$op2 = array("15", "16", "17", "18");
//echo $op1[1];
if(count($op1)> count($op2)){
echo "wrong";
}
else{
echo "right";
}
//ouptput is "right"

我试过这个开关,但都错了。我试了一下,它给出了一个巨大的错误:

   //switch for the if-else

 switch (count($op1)>count($op2)){
case (false):
echo "it is false";
case (true):
echo "it is true";

在输出中,同时显示"it is true"answers"it is flase"。

请给出正确的方法来做这件事。由于

Foreach用于循环语句。

如果你必须一个一个地获取数组的所有值,你可以使用foreach。

foreach($array_val AD $val){//你可以使用$val

}

希望对您有所帮助:

// Use switch for the if-else
switch (count($op1)>count($op2)) {
    case FALSE:
        echo "it is false";
        break;
    case TRUE:
        echo "it is true";
        break;
    // default:
        // default is not required here, as the result is either TRUE or FALSE
}