M
M
mrzgt2015-11-23 13:09:34
PHP
mrzgt, 2015-11-23 13:09:34

How to fix division by zero?

$data['products'][] = array(
......
'saving' => $result['price'] = round((($result['price'] - $result['spec'] )/$result['price'])*100, 0),
);
but if the variable result['price'] is equal to 0, an error is thrown about division by 0, how can I fix it&

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cat Anton, 2015-11-23
@27cm

Do not divide by zero.

O
Optimus, 2015-11-23
Pyan @marrk2

$res = $result['price'] - $result['spec'];
if($res <= 0) { ... // что тогда делать

V
Vit, 2015-11-23
@fornit1917

Do not divide by zero, obviously

if ($result['price']) != 0) {
  $data['products'][] = array(
  ......
    'saving' => $result['price'] = round((($result['price'] - $result['spec'])/$result['price'])*100, 0),
  );
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question