Division by zero warning [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Division by zero warning [SOLVED]

Post by mikeeeeeeey »

Hi,

I have some lines of code...

Code: Select all

$percent = (int)$_POST['search_factor'];
$sum = round($colour+($colour/$percent));
which generates the warning...

Code: Select all

Warning: Division by zero in C:\wamp\.....
and I'm too sure why. If I change $percent to a number e.g. 10, it works fine. Can anyone please help?

Thanks
Last edited by mikeeeeeeey on Mon Jan 21, 2008 11:04 am, edited 1 time in total.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Division by zero warning

Post by Inkyskin »

Try echoing the value of $_POST['search_factor'] first, to make sure it actually has a number in it
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Division by zero warning

Post by VladSun »

What are you trying to achieve?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Re: Division by zero warning

Post by mikeeeeeeey »

If I do a var_dump() of $_POST['search_factor'], it says it is an integer, so if the number was 10 it would output...

Code: Select all

int(10)
and I'm trying to get a user-inputted percentage, then generate two other numbers; (using the example percentage above)

number-(10% of number)

and

number+(10% of number)
Tor Vidvei
Forum Newbie
Posts: 2
Joined: Mon Jan 21, 2008 10:00 am

Re: Division by zero warning

Post by Tor Vidvei »

mikeeeeeeey wrote: ...and I'm trying to get a user-inputted percentage, then generate two other numbers; (using the example percentage above)

number-(10% of number)

and

number+(10% of number)
Shouldn't you then write

Code: Select all

$sum = round($colour+($colour*$percent));
instead of the cited code? (Then your problem vanishes...)

Tor Vidvei
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Re: Division by zero warning

Post by mikeeeeeeey »

Oh yeah! Thanks for your help :)
Post Reply