一种总结许多命令的方法,例如 if... 和 if... do
A way to summarize many commands like if... and if... do
我创建了一个问答页面来确定每个用户对不同产品的兴趣程度
像这样:
你有多喜欢 x (betwin 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.
有关 if
和 switch
之间差异的更多信息由 Masivuye Cokile 在您的问题中作为评论提供:Which is Faster and better, Switch Case or if else if?
希望对您有所帮助。让我知道。
我创建了一个问答页面来确定每个用户对不同产品的兴趣程度
像这样:
你有多喜欢 x (betwin 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.
有关 if
和 switch
之间差异的更多信息由 Masivuye Cokile 在您的问题中作为评论提供:Which is Faster and better, Switch Case or if else if?
希望对您有所帮助。让我知道。