Create a page in PHP that will display a random math question for kids. It shoul
ID: 3819870 • Letter: C
Question
Create a page in PHP that will display a random math question for kids. It should have 2 random numbers and a random operator (Should use division, multiplication, addition, and subtraction). Keep the random numbers between 1-10 for school kids.
Example:
random # 1: 4
random # 2: 10
random operator "+"
Page would display a form with the text 4+10= <input> and a submit button.
When the user enters in a number and submits, reload the same page and let them know if they answered it correctly. If they did not, show them the correct answer.
Everytime you load this page you should get a new math problem to solve.
Explanation / Answer
<?php
echo '<form action="" method=post>';
echo '<input type="text" name="text1" value="' . rand (0,10) . '">';
echo '<br>';
echo '<br>';
echo '<input type="text" name="text2" value="' . rand (0,10) . '">';
echo '<br>';
echo '<br>';
echo 'Enter the result: ';
echo '<br>';
echo '<br>';
echo '<input type="text" name="result">';
echo '<br>';
echo '<br>';
echo '<input type="submit" value="Submit">';
echo '</form>';
if(isset($_POST["result"]))
{
$r=$_POST["result"];
$correct =$_POST["text1"]+$_POST["text2"];
if($r==($_POST["text1"])+$_POST["text2"])
{
echo "Correct result";
}
else
{
echo "wrong result. The correct result is : $correct Please try another question displayed.";
}
}
else{
echo "Please enter the result";
}
?>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.