PHP round function?

Basically… on my new project a user can earn points and cash these points in for vouchers.

5 points = $1.00 USD.

I have been looking around for a PHP round function, or something similar to do the job, but basically this is what I’ve come up with.

100 points = $20
250 points = $50
500 points = $100
1,000 points = $200

If they don’t have the exact amount of points, they can’t get such voucher.

For example: User has 249 points, he can only get a $20 voucher until he gets 1 more point… it will then display he can get a $20 voucher with 100 points, and 149 points remaining.

This is my code, pretty basic:


<?
$points = $_GET['pts'];

if ($points < 100){
	$amt = 0;
}elseif (($points >= 100) && ($points < 250)){
	$amt = 20;
}elseif (($points >= 250) && ($points < 500)){
	$amt = 50;
}elseif (($points >= 500) && ($points < 1000)){
	$amt = 100;
}elseif ($points >= 1000){
	$amt = 200;
}

$leftover = $points - ($amt * 5);
echo "Your card is worth <strong>$" . $amt . ".00</strong> with " . $leftover . " points left over<br />";
?>

Would that work? Are there any premade functions which would suit it better?

I would work, but I would do it something like this


$points = $_GET['pts'];

$rewards = array(
  100 => 20,
  250 => 50,
  500 => 100,
  1000 => 200
);

$amt = 0;
foreach($rewards as $pts => $am) {
  if ($pts >= $points) {
    $amt = $am;
  } else {
    break;
}

$leftover = $points - ($amt * 5);
echo "Your card is worth <strong>$" . $amt . ".00</strong> with " . $leftover . " points left over<br />";

Suppose you have 249 points. $amt now starts at 0.
It looks if you have 100 points or more, you do, so $amt=20.
Now it looks if you have 250 points or more, you don’t, so the foreach loop breaks, and you have $amt=20, which is what you want.
Note that for this approach to work the keys in $rewards need to be sorted ascending. For example, the following doesn’t work:


$rewards = array(
  500 => 100,
  100 => 20,
  250 => 50,
  1000 => 200
);

Because the loop will start to see if you have 500 points or more, you don’t, so you’re stuck with $amt = 0

BTW. I’m hoping you will implement the points in more secure way than just $_GET[‘pts’] ? This way I can just go to your website and change ?pts=some-value to ?pts=1000000 and I have 1 million points!

Hope you will get some assitance from following example:

<?php
echo round(3.14159265);
// Value would be 3

echo round(3.9);
// Value would be 4

echo round(3.14159265, 3);
// Value would be 3.142

echo round(314159, -100);
// Value would be 314100
?>

Hi, The get points thing was just an example, $points is actually a variable taken from their account info on the database.

Would it be better if, say they have 200 points… that actually works out to 2 x $20 vouchers?

What would be the best way to say You can get 2 x $20.00 vouchers as you have enough points, along those lines?

@Jooms, the round thing only seems to work for fractions afaik?

What you would then one to do is floor to the nearest 100-number.

That is actually pretty easy, you can use


floor(pts / 100) * 100

Try it:


$pts = 249;
echo floor($pts / 100) * 100; //200

So 200 of the 249 points can be used. Meaning you get 200/100 = 2 vouchers, and you have 249-200 = 49 points remaining.

See the PHP manual for the floor function

To be more elaborate, you could do


function checkVouchers($points, $divider) {
  if ($divider > $points) return array(0,$points);
  $tmp = floor($points / $divider) * $divider;
  $vc = $tmp/$divider;
  $remaining = $points - ($vc * $divider);
  return array($vc, $remaining);
}

$points = 1851;
$voucers = array();
$dividers = array(1000,500,250,100);
foreach($dividers as $div) {
  $tmp = checkVouchers($points,$div);
  $vouchers[$div] = $tmp[0];
  $points = $tmp[1];
}

foreach($vouchers as $divider => $num) {
  if ($num == 0) continue;
  echo 'You can get ', $num, ' vouchers for ', ($divider / 5), '$', '(', $divider, ')', "\
";
}
echo 'You have ', $points, ' point', ($points!=1?'s':''), ' remaining';

This outputs:


You can get 1 vouchers for 200$
You can get 1 vouchers for 100$
You can get 1 vouchers for 50$
You can get 1 vouchers for 20$
You have 1 points remaining	

Thanks for your advice mate, been a great help!