Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

E.g. 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.

E.g.

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

Can anyone help me?

Thanks.

like image 759
Seth-77 Avatar asked May 07 '13 14:05

Seth-77


4 Answers

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

if($value >=500 && $value <=600 )
{
  echo "value is between 500 and 600";
}
like image 171
raidenace Avatar answered Oct 24 '22 00:10

raidenace


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';
}
like image 29
Frédéric Clausset Avatar answered Oct 24 '22 01:10

Frédéric Clausset


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.

like image 1
Langdon Avatar answered Oct 23 '22 23:10

Langdon


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"
like image 1
SteeveDroz Avatar answered Oct 24 '22 01:10

SteeveDroz