Advertisement
Shaun_B

How to divide by zero in PHP without causing issues

Oct 23rd, 2018
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.25 KB | None | 0 0
  1. <?php
  2.  
  3. $divisor = 0;
  4. $dividend = 16;
  5.  
  6. // Wrong
  7. $quotient = $dividend / $divisor;
  8. echo $quotient . PHP_EOL;
  9.  
  10. // Correct
  11. if( $divisor > 0 ) {
  12.     // Does not execute in this case
  13.     $quotient = $dividend / $divisor;
  14.     echo $quotient . PHP_EOL;
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement