-4

我有变量,例如:

$type1=10;
$type2=40;
$type3=70;
   .
   .
   .
 and more

但是这个变量并不稳定,因为我是从表单中获取它们的。

而且我还有一个变量:

$total=30;

我想要

 if `$total` less than `$type1` =>show special message.
 if `$total` between `$type1` & `$type2` =>show special message2.
 if `$total` between `$type2` & `$type3` =>show special message3.
          and more...
4

2 回答 2

0

通常的 if 语句有什么问题?如果您从表单中获取值,您可以简单地将这些值分配给不同的变量,并在您的if语句中使用它来进行比较。

$type1= $_POST['type3'];
$type2= $_POST['type3'];
$type3= $_POST['type3'];

if($total < $type1) {
    //show special message
}
if($total < $type2 && $total1 > $type1) {
    //show special message 2
}
if($total < $type3 && $total > $type2) {
    //show special message 3
}
if(...) {
...
}
于 2013-08-19T11:16:03.383 回答
0
$type1=10;
$type2=40;
$type3=70;

$total=30;

$n=10;//count of variables you have
for ($i=0; $i <= $n; $i++) { 
    $v = $GLOBALS['type'.$i];
    $nv = (isset($GLOBALS['type'.$i+1])?isset($GLOBALS['type'.$i+1]):false);
    if($total < $v){
        echo '$total is lesser than $type'.$i;
    }elseif(isset($nv) && $v < $total && $total < $nv){
        echo '$total is between $type'.$i.'and $type'.$i+1;
    }
}
于 2013-08-19T11:26:03.697 回答