Marie Posted September 6, 2022 Share Posted September 6, 2022 I have a form on one page that submits to another page. There, it checks if the number is divisible by another number. Please check the code, What is missing or wrong? <?php if(isset($_POST['submit'])) { if(is_numeric($_POST['num1']) && is_numeric($_POST['num2'])) { if($_POST['operation'] == 'divided by') { if ($num1 % $num2 == 0) { echo $_POST['num1'] . ' Is Divisible By ' . $_POST['num2']; } else { echo $_POST['num1'] . ' Is Not Divisible By ' . $_POST['num2']; } echo "<h1>{$_POST['num1']} {$_POST['operation']} {$_POST['num2']} equals {$answer}</h1>"; } else { echo 'Numeric values are required'; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315281-need-help-in-checking-divisibility-of-numbers-in-php/ Share on other sites More sharing options...
Barand Posted September 6, 2022 Share Posted September 6, 2022 You seem to missing the bit where $answer is calculated. There should be an error message telling you that $anwer is not defined. Quote Link to comment https://forums.phpfreaks.com/topic/315281-need-help-in-checking-divisibility-of-numbers-in-php/#findComment-1600187 Share on other sites More sharing options...
Marie Posted September 7, 2022 Author Share Posted September 7, 2022 I've fixed the issue. thank you Quote Link to comment https://forums.phpfreaks.com/topic/315281-need-help-in-checking-divisibility-of-numbers-in-php/#findComment-1600209 Share on other sites More sharing options...
dianacrown88 Posted November 19, 2022 Share Posted November 19, 2022 On 9/6/2022 at 8:33 AM, Marie said: I have a form on one page that submits to another page. There, it checks if the number is divisible by another number. Please check the code, What is missing or wrong? <?php if(isset($_POST['submit'])) { if(is_numeric($_POST['num1']) && is_numeric($_POST['num2'])) { if($_POST['operation'] == 'divided by') { if ($num1 % $num2 == 0) { echo $_POST['num1'] . ' Is Divisible By ' . $_POST['num2']; } else { echo $_POST['num1'] . ' Is Not Divisible By ' . $_POST['num2']; } echo "<h1>{$_POST['num1']} {$_POST['operation']} {$_POST['num2']} equals {$answer}</h1>"; } else { echo 'Numeric values are required'; } } } ?> To check if a number is not a divisible of another number, we can use the modulo operator % but the remainder of first number by second number is not equal to 0. Quote Link to comment https://forums.phpfreaks.com/topic/315281-need-help-in-checking-divisibility-of-numbers-in-php/#findComment-1602729 Share on other sites More sharing options...
Barand Posted November 19, 2022 Share Posted November 19, 2022 Brilliant, Sherlock! If you had taken the trouble to read the (old and already fixed) post you would have seen that it already uses the modulo operator. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315281-need-help-in-checking-divisibility-of-numbers-in-php/#findComment-1602730 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.