Warning: Division by zero – easy way to get arround?

September 26, 2014
$sum = [0, 0, 0, 0];

echo count($sum) / array_sum($sum);
// Warning: Division by zero

$arraySum = 0.00001 + array_sum($sum);
$number = count($sum) / $arraySum;
echo round($number, 2);
// Result is 0;

Just add some small number to the one you are not sure that it will be bigger than zero.
Easy as that.

posted in php by Ivan Gospodinow

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment


Warning: Use of undefined constant XML - assumed 'XML' (this will throw an Error in a future version of PHP) in /home/c2kblate/sites/ivangospodinow.com/wp-content/plugins/wp-syntaxhighlighter/wp-syntaxhighlighter.php on line 1048
 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org