我如何在变量之间显示消息?

时间:2013-08-19 11:13:10

标签: php variables

我有变量,例如:

$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...

2 个答案:

答案 0 :(得分: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(...) {
...
}

答案 1 :(得分: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;
    }
}