-1

Code 1 for checking input are numeric

 if ($_SERVER["REQUEST_METHOD"] == "POST"):
    if(!preg_match("/^[0-9]+$/", $_POST['number1'])):
        $numerr1 = "Please Enter number only";
    else:
        $num1 = $_POST['number1'];
    endif;
    if(!preg_match("/^[0-9]+$/", $_POST['number2'])):
        $numerr2 = "Please Enter number only";
    else:
        $num2 = $_POST['number2'];

        $sum = sum($num1, $num2);
        $subtract = subtract($num1, $num2);
        $divide = divide($num1, $num2);
        $multiply = multiply($num1, $num2);
    endif;

endif;

code 2 to print results

if (isset($_POST['sum'])):
        echo "Sum of $num1 and $num2 is: $sum";
endif;

if (isset($_POST['subtract'])):
        echo "Subtraction of $num1 and $num2 is: $subtract";
endif;

if (isset($_POST['divide'])):
        echo "Division of $num1 and $num2 is: $divide";
endif;

if (isset($_POST['multiply'])):
        echo "Multiplication of $num1 and $num2 is: $multiply";
endif;

the thing is Every time i enter 2nd number as zero it takes me to a blank screen .... how can i fix this?

Rao DYC
  • 13
  • 1
  • 3
  • 3
    Turn on error reporting [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) (Division by zero is a no-no btw) – brombeer May 11 '21 at 10:32
  • 2
    That depends on what you mean by "fix"? Do you want to get meaningful debugging output instead of a blank screen? Do you want to handle errors more effectively? Do you want to check if the input is `0` before performing the operation that's failing? Do you want to enter a number that isn't `0`? Something else? It seems pretty likely that the problem happens when you try to divide by `0`, and some debugging on your part can confirm that. From there, what exactly are you trying to do? – David May 11 '21 at 10:36

1 Answers1

0

Validate your inputs

You are experiencing a White Screen Of Death (WSOD). This happens because dividing by zero is mathematically not possible so the program cannot execute correctly and quits.
As suggested by other comments, you can make PHP display errors to figure this out yourself in the future.

You must check all inputs if they are valid. If Someone enters number 2 as 0, your program must handle this with giving out an error message in case of the division.

This is true for all programming forever so it is a good idea you learn how to validate variables. They can not only cause errors, they could even cause security breaches into your system.

I suggest working through some PHP tutorials and learning along the way.

Peter Krebs
  • 749
  • 7
  • 17
  • This is what I did `function divide($num1, $num2) { if($num2 == 0){ return "Division by Zero not possible"; }else{ return ($num1 / $num2); } }` – Rao DYC May 11 '21 at 12:12
  • Sure that's one example for simple error handling. A little cooler is throwing an Exception with that message to make extra clear your program doesn't feel well: `throw new Exception('Division by zero.');`. It is a little overkill in a small example program to do that, though. – Peter Krebs May 11 '21 at 12:24