我创建了一个问答页面来确定每个用户对不同产品的兴趣程度,例如:
你喜欢 x 的程度(1-10 之间)
这些问题超过 30 个,如果我想写一个每种可能性的命令行,几乎是不可能的。
命令是这样的:
if $a <=5 and $b <=6 and $c <=7 and... do ...
if $a<= 8 and $b <=7 and $c >= 5 and $d <=8 do...
我希望命令以这种方式工作
有没有更好的方法来做到这一点?
谢谢
为此,您可以使用switch
声明。文档: http: //php.net/manual/en/control-structures.switch.php
示例代码:
$i = 10;
switch($i):
case ($i <= 0): // or for example: case (0)
return 'It\'s 0 or lower than 0';
break;
case ($i > 0 && $i < 10):
return 'Number is between 0 and 10';
break;
case ($i >= 10):
return 'Number is 10 or higher';
break;
default:
return false;
endswitch;
// You can use echo instead of return, but i prefer to use these statements in a function instead of the html page.
Masivuye Cokile 在您的问题中提供了有关if
和之间差异的更多信息作为评论:哪个更快更好,Switch Case 还是 if else if?switch
我希望这会有所帮助。让我知道。