i am having issues when 2nd input (num2) is entered zero

Tags:



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?

Answer

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.



Source: stackoverflow