If value is greater/lesser than xyz

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.

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

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.

Eg

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

Can anyone help me?

Thanks.


if conditions PHP中的if conditions是最严谨,最新和最广泛使用的语法是:

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

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.


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

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"
链接地址: http://www.djcxy.com/p/10034.html

上一篇: 比较数字字符串

下一篇: 如果值大于/小于xyz