php 如果值大于/小于 xyz

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16421617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:05:31  来源:igfitidea点击:

If value is greater/lesser than xyz

phpif-statement

提问by Seth-77

I have a value as a number. For instance, 502. I want to write a php if statement that will display some text if the value is lesser or greater than certain numbers, or between a range.

我有一个数字的价值。例如,502。我想编写一个 php if 语句,如果该值小于或大于某些数字,或介于某个范围之间,它将显示一些文本。

E.g. number is 502, text will say: "Between 500-600" number is 56, text will say: "Between 0-60" etc.

例如数字是 502,文本会说:“500-600 之间” 数字是 56,文本会说:“0-60 之间”等等。

So far I have this:

到目前为止,我有这个:

<?php $count=0;?>
<?php $board = getUserBoard($userDetails['userId']);?>
<?php if(is_array($board)):?>
<?php  $boardCount = count($board);?>
<?php foreach($board as $key=>$value):?>
<?php
$boardPin = getEachBoardPins($value->id);
$count = $count + count($boardPin);
?>
<?php endforeach?>
<?php endif?>

And that gives me a number:

这给了我一个数字:

<?php echo $count;?>

I have tried writing...

我试过写...

<?php if(($count)): => 500 ?>
Over 500
<?php endif ?>

But I keep running into errors.

但我一直遇到错误。

I'd like to create a list if possible with elseif statements denoting various number ranges.

如果可能,我想创建一个列表,用 elseif 语句表示各种数字范围。

E.g.

例如

0-50, 51-250, 251-500 etc.

0-50、51-250、251-500 等

Can anyone help me?

谁能帮我?

Thanks.

谢谢。

回答by raidenace

The sanest, neatest and most widely used syntax for if conditionsin PHP is:

if conditionsPHP 中最清晰、最简洁和最广泛使用的语法是:

if($value >=500 && $value <=600 )
{
  echo "value is between 500 and 600";
}

回答by Frédéric Clausset

if ($count >= 0 && $count < 100) {
    echo 'between 0 et 99';
} elseif ($count < 199) {
    echo 'between 100 and 199';
} elseif { ...

}elseif ($count < 599) {
    echo 'between 500 and 599';
} else {
    echo 'greater or equal than 600';
}

回答by Langdon

I wrote something like this a few years back (might be a better way to do it):

几年前我写了这样的东西(可能是更好的方法):

function create_range($p_num, $p_group = 1000) {
    $i = 0;

    while($p_num >= $i) {
        $i += $p_group;
    }

    $i -= $p_group;

    return $i . '-' . ($i + $p_group - 1);
}

print 'The number is between ' . create_range(502, 100) . '.';

It'll say 500-599, but you can adjust it to your needs.

它会说 500-599,但您可以根据需要进行调整。

回答by SteeveDroz

I'm not sure what you need, but here is what I understand you ask:

我不确定你需要什么,但这是我理解你问的:

function getRange($n, $limit = array(50, 250, 500)) { // Will create the ranges 0-50, 51-250, 251-500 and 500-infinity
  $previousLimit = 0;
  foreach ($limits as $limit) {
    if ($n < $limit) {
      return 'Between ' . ($previousLimit + 1) . ' and ' . $limit; //Return whatever you need.
    }
    $previousLimit = $limit;
  }
  return 'Greater than ' . $previousLimit; // Return whatever you need.
}

echo getRange(56); // Prints "Between 51 and 250"
echo getRange(501); // Prints "Greater than 500"
echo getRange(12, array(5, 10, 15, 20)); // Prints "Between 11 and 15"

回答by qisho

function getRange($number){
    $length=strlen($number);
    $length--;
    $r1=round($number,-$length);
    if ($r1>$number){
        $r2=$r1-pow(10,$length);
    return ''.$number.' value is between '.$r2.'-'.$r1;
    }
    else {
    $r2=$r1+pow(10,$length);
        return  ''.$number.' value is between '.$r1.'-'.$r2;
    }
 }

Try this.

尝试这个。