Error Division By Zero In Php
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us
Warning Division By Zero In Php
Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community error division by zero sql Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to error division by zero postgresql suppress the “Division by zero” error and set the result to null for the whole application? up vote 6 down vote favorite How to suppress the "Division by zero" error and set the result to null for the whole application? By saying
Php Division By Zero Exception
"for the whole application", I mean it is not for a single expression. Instead, whenever a "Division by zero" error occurs, the result is set to null automatically and no error will be thrown. php share|improve this question edited Sep 16 '10 at 23:58 asked Sep 16 '10 at 23:47 Ethan 9,97082948 If you're looking for c++ operator overloading possibility, so the php doesn't support it, see the discussion here: stackoverflow.com/questions/787692/operator-overloading-in-php –Igor Sep 17 '10 at 0:27 Suppressing errors is generally
How To Solve Division By Zero In Php
considered a bad practice. You can use try and catch, but not @. You should write your code in such way that incorrect values are sanitized and no errors or warnings are thrown by standard execution of your code. –Mike Sep 26 '13 at 14:53 in SQL Server, there is a little trick NULLIF() bennadel.com/blog/… –Jaider Apr 29 '14 at 20:24 add a comment| 4 Answers 4 active oldest votes up vote 15 down vote accepted This should do the trick. $a = @(1/0); if(false === $a) { $a = null; } var_dump($a); outputs NULL See the refs here error controls. EDIT function division($a, $b) { $c = @(a/b); if($b === 0) { $c = null; } return $c; } In any place substitute 1/0 by the function call division(1,0). EDIT - Without third variable function division($a, $b) { if($b === 0) return null; return $a/$b; } share|improve this answer edited Jul 9 '14 at 13:03 Community♦ 11 answered Sep 16 '10 at 23:52 Igor 1,59131536 Wow thats so simple (I wish I thought of it) but does it set it to null? –Mark Lalor Sep 16 '10 at 23:57 mmm right, but I'm still stumped by his edit :P –Mark Lalor Sep 17 '10 at 0:02 2 This is the best general concept. However, it fails when a == 0. You should change the conditional to be if ($c === false). But actually, you should just check if $b == 0. ;) –Matthew Sep 17 '
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About php suppress division by zero warning Us Learn more about Stack Overflow the company Business Learn more about hiring
How To Remove Warning: Division By Zero In Php
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the message division by zero Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PHP, How to catch a division http://stackoverflow.com/questions/3731710/how-to-suppress-the-division-by-zero-error-and-set-the-result-to-null-for-the by zero? up vote 18 down vote favorite 2 I have a large mathematical expression that has to be created dynamically. For example, once I have parsed "something" the result will be a string like: "$foo+$bar/$baz";. So, for calculating the result of that expression I'm using the eval function... something like this: eval("\$result = $expresion;"); echo "The result is: $result"; The problem here is that sometimes http://stackoverflow.com/questions/3071067/php-how-to-catch-a-division-by-zero I get errors that says there was a division by zero, and I don't know how to catch that Exception. I have tried things like: eval("try{\$result = $expresion;}catch(Exception \$e){\$result = 0;}"); echo "The result is: $result"; Or: try{ eval("\$result = $expresion;"); } catch(Exception $e){ $result = 0; } echo "The result is: $result"; But it does not work. So, how can I avoid that my application crashes when there is a division by zero? Edit: First, I want to clarify something: the expression is built dynamically, so I can't just eval if the denominator is zero. So... with regards to the Mark Baker's comment, let me give you an example. My parser could build something like this: "$foo + $bar * ( $baz / ( $foz - $bak ) )" The parser build the string step by step without worrying about the value of the vars... so in this case if $foz == $bak there's in fact a division by zero: $baz / ( 0 ). On the other hand as Pete suggested, I tried:
New? Today's Posts FAQ Rules Guidelines Search Advanced Search Forum :: Server side development PHP How do I http://www.codingforums.com/php/2404-how-do-i-avoid-division-zero-error.html avoid division by zero error? If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection division by below. Enjoy an ad free experience by logging in. Not a member yet? Register. Results 1 to 9 of 9 Thread: How do I avoid division by zero error? Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Rate This Thread Current Rating Excellent Good Average Bad Terrible 07-20-2002,10:47 PM #1 mouse division by zero View Profile View Forum Posts Visit Homepage Regular Coder Join Date Jun 2002 Location North East England Posts 853 Thanks 0 Thanked 0 Times in 0 Posts How do I avoid division by zero error? Warning: Division by zero in... I'm trying to calculate a percentage using... $percent1 = round(($num_rows1*100)/$num_rows_total); $percent2 = round(($num_rows2*100)/$num_rows_total); and so on... what's causing this error and what can I do to prevent it? [+] Computer/PC issues [+] Silverpaw3D ------------------------------------------------ Never buy a dwarf with learning disabilities... ...it's not big, and it's not clever. Reply With Quote 07-20-2002,10:56 PM #2 Spookster View Profile View Forum Posts Supreme Overlord Join Date May 2002 Location Marion, IA USA Posts 6,298 Thanks 4 Thanked 84 Times in 83 Posts Well that's easy.....stop dividing by zero. Ok well if your num_rows_total variable is equal to zero then obviously you will get this error. Are these rows referring to records in a database? You could put a conditional statement in there: if($num_rows_total!=0) $per