Boneheaded PHP math question

Let's say you wanted to figure out how many cars a group needs to rent.

$capacity is the number of people the car can hold
$people is the number of people in your group

My first thought on how to approach it would be to do something like

while ($people > 0) {
$cars ++;
$people = $people - $capacity;
}

That way it just kinda iterates up the number of cars as groups.  Say car capacity is 5... If there's one person, it only occurs once.  If there's 6 people, it cycles twice, 2 cars...etc.

Is there a better way you think of approaching that?  Just feels kinda sloppy to me doing it that way.
LVL 2
erzoolanderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Radek BaranowskiFull-stack Java DeveloperCommented:
you might use modulo
if ($people % $capacity ==0)
{
$cars = $people/$capacity
}
else
{
$cars= ceil($people/$capacity);
}

Open in new window


ot just use

ceil($people/$capacity)
0
Radek BaranowskiFull-stack Java DeveloperCommented:
yeah, ceil() will just do!

http://php.net/manual/en/function.ceil.php
0
KimputerCommented:
For simple math, the while loop makes it harder to read what you exactly mean:

<?php

$people = 11;
$cars = 5;
if (($people % $cars) > 0) {
echo (floor($people / $cars) + 1);

}

else {
echo (floor($people / $cars));
}

?>

Open in new window

0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Radek BaranowskiFull-stack Java DeveloperCommented:
sample code:

<?php

echo "5 people: " . ceil(5/5)." car(s)";
echo "<br>";
echo "7 people: " . ceil(7/5)." car(s)";

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KimputerCommented:
I used the wrong titles btw (cars  should be capacity, echo is cars). And that's besides the fact that the other expert's solution is neater.
0
erzoolanderAuthor Commented:
Gracias :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.