PHP division by zero error -
i did research division 0 error in php. found couple of of them said if statement did code below, still getting error, can tell me what's wrong code?
<?php $result = array( 'price' => 0, 'special' => 80 ); $product = array( 'savings' => round((($result['price'] - $result['special'])/$result['price'])*100, 0) ); if ($result['price'] == 0) { echo $result['price']; } else { echo "you save " . $product['savings'] ."%"; } ?>
i tried both
if == 0 , if != 0
as new php, won't if statement mean if price of product = 0 echo out price , if it's not echo out special price?
i'm not allowed move $product
array.
this seems make more sense, money-wise: check first if price higher special, , if so, perform calculation.
$product = array( 'savings' => ($result['price'] > $result['special'] ? (round((($result['price'] - $result['special'])/$result['price'])*100, 0)) : 0) );
Comments
Post a Comment