我试图为变量赋值,我想知道是否有更方便和更简单的东西,通常的if / else if方式。
$typeofvariable = rand (1,9);
if ($typeofvariable == 1) {
$stringvalue = "type a";
}
else if ($typeofvariable == 2) {
$stringe value == "type b";
}
.......
提前谢谢。
答案 0 :(得分:0)
您可以使用switch
声明
$typeofvariable = rand (1,9);
switch ($typeofvariable) {
case 1:
$stringvalue = "type a";
break;
case 2:
$stringvalue = "type b";
break;
case 3:
// etc..
break;
default:
// Code to be executed if non of above.
break;
}
答案 1 :(得分:0)
您可以像这样使用switch()
:
<?php
$typeofvariable = rand (1,9);
switch ($typeofvariable) {
case 1:
//code to be executed if $typeofvariable=1;
break;
case 2:
//code to be executed if $typeofvariable=2;
break;
case 3:
//code to be executed if $typeofvariable=3;
break;
...
default:
code to be executed if $typeofvariable is different from all labels;
}
?>
答案 2 :(得分:0)
因为这就是堆栈的工作原理。
这应该适合你:
<?php
$map = array("type a", "type b", ...);
$string = $map[array_rand($map)];
?>