1

I am doing a calculation with an array which is drawn from mysql and the problem is "division by zero" warning

I am running a loop over this statment

$v = (($v - $valuei)/($valuei) * 100); 

Here $v is is changing (let's say from 100 to 150 e.g 100, 101, 102, 103,...,150) and $valuei is the intial value of $v (i.e 100) So, when the calculation will start first output should be zero and I want that zero then why it gives a warning "Division By Zero " and also makes all the "$v" values equal to zero.

2
  • 1
    Why on earth are you multiplying by a numeric string? I mean, sure it'll work anyway... but why? Commented Jul 21, 2012 at 13:37
  • Integers are too mainstream I guess :) Commented Jul 21, 2012 at 13:41

2 Answers 2

4

The warning suggests, that $valuei is, in fact, 0 during one of the runs.

Are you sure, that $valuei is initialized properly? To get rid of the warning, you should add do:

if($valuei != 0) {
    $v = (($v - $valuei)/($valuei) * '100');
}

This way, the warning should disappear.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You Anthony! You were right, Error was due to O value of $valuei itself..... Everything working perfectly now......
1

If $valuei is initially set to $v then on the first iteration (x - x) = 0 and thus, you are dividing by zero (or rather, are trying to divide 0 by something, which is a mistake).

Without seeing the rest of the loop I can only speculate, but I suspect the direction you're probably heading for is to start the loop at index '1' instead of '0'.

Also, why multiply by '100' instead of 100?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.