我被困在一个php代码,我的问题是:
我正在使用php if和elseif来显示如下内容:
<?php
$test_a = 0 to 10; (My problem)
$test_b = 11 to 20; (My problem)
$test_c = 21 to 30; (My problem)
$test = 50; (random value)
$something = 12;
$something_b = 5;
$something_c = 15;
?>
<?php
if($test>=$test_a){
echo $something;
}elseif ($test=<$test_b) {
echo $something_b;
}elseif (test=<$test_c) {
echo $something_c;
}
?>
我的问题是如何使用if/elseif的范围。
谢谢!
这显然不是有效的语法。这实际上只是一个基本的if/else语句:
if($test>=0 && $test <=10){
echo $something;
}elseif ($test>=11 && $test <=20) {
echo $something_b;
}elseif ($test>=21 && $test <=30) {
echo $something_c;
}
检查$test
大于等于1或小于等于2。如果没有,继续下一个If语句。
将变量设置为每个范围的顶部:
$test_a = 10;
$test_b = 20;
$test_c = 30;
if ($test <= $test_a) {
echo $something;
} elseif ($test <= $test_b) {
echo $something_b;
} elseif ($test <= $test_c) {
echo $something_c;
}
如果有很多情况,最好创建一个数组并使用循环:
$tests = array (array ('limit' => 10, 'message' => $something),
array ('limit' => 20, 'message' => $something_b),
array ('limit' => 30, 'message' => $something_c)
);
foreach ($tests as $curtest) {
if ($test <= $curtest['limit']) {
echo $curtest['message'];
break;
}
}