Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Ben Taylor
Ben Taylor
902 Points

PHP If Statement

Hey guys,

I was wondering if anyone could help.. In my shopping cart I've put in a function to round products up to the nearest 5p.. The function is:

function roundFive($v) {

        return ceil($v*20) / 20;

Which works great, however it's also rounding up products that don't need to be rounded up.. So basically I need to add an if statement to it.. The term basically needs to say "if price does not end in 0 or 5.. round up to nearest 5p, it does, do nothing"

But I'm not very good with php.. can anyone help??

Big thanks!


3 Answers

Hi Ben

You might be better off using php's built in round function, which allows you to define the level of precision required.


$v = 5.045;

return round($v, 2);    //returns  5.05

function round Bob

Ben Taylor
Ben Taylor
902 Points

Hi Bob,

I looked at round initially, but I need it to round up everytime.. not round to the nearest...

Hi Ben,

Okay now I'm a little confused as to what you are trying to achieve?

Lets see if we are on the same page, if I had a list of values:

2.59 3.40 4.88

You would want the first and last to be rounded up to 2.60 and 4.90? but leave the second as 3.40?


So, I think I'm in the same boat as robertkingstone, a little confused on what you're trying to achieve. I've come up with something, but there's probably a better way to doing this for sure, so I would encourage you to maybe google around a little bit.

What I've come up with is passing the value into the function, changing it to a string, then checking the value of the last number in the string. I think you only need one conditional, since ceil of 0 I believe is 0, so there's nothing for it round up. Check out below and see if points you in the right direction. I think I would have to agree with robertkingstone that "round" is probably a better function to use than "ceil", especially when working with products. Plus you have more control of the rounding process. Check out the php manual on round.

$startnumber = 20.05;

function roundFive($v) {
    $checkfloat = strval($v);
    $checkfloat = substr($v, -1);

    if($checkfloat != 5) {
      return ceil($v*20) / 20;
    } else {
      return $v;