ShoeLace1291 Posted October 27, 2011 Share Posted October 27, 2011 So, this is something that I've been wondering for a while now... say you have two variables that you need to divide to find a certain percentage and one happens to be zero... why does PHP feel the need to return a warning saying there was a division by zero? why not just return the number zero since anything that's divided by zero always results in zero anyway? Just a random thought. Quote Link to comment https://forums.phpfreaks.com/topic/249894-php-division-by-zero/ Share on other sites More sharing options...
jotorres1 Posted October 27, 2011 Share Posted October 27, 2011 That not true, division by zero is NOT equal to zero. Just basic math. Example, you have 3 / 0 != 0 because 0 x 0 != 3. You get the point. In which case, in PHP you can use the Error Suppression Operator, something like this. $a = @(25/0); Using the '@' Operator, the error is suppressed. Quote Link to comment https://forums.phpfreaks.com/topic/249894-php-division-by-zero/#findComment-1282618 Share on other sites More sharing options...
Kieran Menor Posted October 27, 2011 Share Posted October 27, 2011 Or even better, you should do a check to see if your divisor is zero so you can handle the situation correctly. Quote Link to comment https://forums.phpfreaks.com/topic/249894-php-division-by-zero/#findComment-1282619 Share on other sites More sharing options...
jotorres1 Posted October 27, 2011 Share Posted October 27, 2011 Yea, these division by zeros are common questions asked in any programming job interviews. Division by zero should always be handled no matter the language. Quote Link to comment https://forums.phpfreaks.com/topic/249894-php-division-by-zero/#findComment-1282621 Share on other sites More sharing options...
The Little Guy Posted October 27, 2011 Share Posted October 27, 2011 $number = 100; if($me == 0) $result = 0; else $result = $number / $me; echo $result; Quote Link to comment https://forums.phpfreaks.com/topic/249894-php-division-by-zero/#findComment-1282678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.