I'm trying to assign a value to a variable and I was wondering if there were something more convenient and short that the usual if / else if way.
$typeofvariable = rand (1,9);
if ($typeofvariable == 1) {
$stringvalue = "type a";
}
else if ($typeofvariable == 2) {
$stringe value == "type b";
}
.......
Thank you in advance.
Since this is how stack works.
This should work for you:
<?php
$map = array("type a", "type b", ...);
$string = $map[array_rand($map)];
?>
You could use a switch
statement
$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;
}
You can use switch()
like this:
<?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;
}
?>