Locked lesson.
About this lesson
Let's write some code to determine if the answer is right or wrong.
Exercise files
Download this lesson’s related exercise files.
Right and Wrong Logic.docx59.4 KB Right and Wrong Logic - Solution.docx
59.7 KB
Quick reference
Right and Wrong Logic
Now it's time to determine the right answer and test our answer vs the right answer.
When to use
We'll use a basic if/else statement.
Instructions
Our code so far...
<html>
<head>
<title>Math Flashcards!</title>
</head>
<body>
<center>
<h1>Addition</h1>
<h1>
<?php
$num1 = rand(1,10);
$num2 = rand(1,10);
echo $num1 . " + " . $num2;
?>
</h1>
<br/><br/>
<?php
$correct_answer = $_POST["num1"] + $_POST["num2"];
if($correct_answer == $_POST["answer"]){
echo "Correct! " . $_POST["num1"] . " + " . $_POST["num2"] . " = " . $_POST["answer"];
}else {
echo "Wrong! " . $_POST["num1"] . " + " . $_POST["num2"] . " = " . $correct_answer . " Not " . $_POST["answer"];
}
?>
<br/><br/><br/>
<form method="post" action="/">
<input name="num1" type="hidden" value="<?php echo $num1; ?>">
<input name="num2" type="hidden" value="<?php echo $num2; ?>">
Answer: <input name="answer"> <button>Submit</button> <button>New Card </button>
</form>
</center>
</body>
</html>
Hints & tips
- First determine what the right answer should be
- Then test your answer against the right answer with an if/else statement
- 00:04 We have our form and it can submit our answer.
- 00:07 And it can keep track of the old numbers.
- 00:10 But now we need to actually create some logic to determine whether
- 00:14 the answer is correct or not.
- 00:16 And one quick note I wanna say before we go forward.
- 00:19 I've done these with these two hidden value input fields.
- 00:20 You can also use session variables to do that, probably even easier.
- 00:23 I've already taught you how to use session variables,
- 00:25 so in the last video I thought I would use these hidden form values
- 00:29 just as another way to pass variables around to different pages of the forms.
- 00:33 But do it this way or
- 00:35 do it with session variables, whatever you're most comfortable with.
- 00:37 Okay, so let's go forward here.
- 00:39 And right here, I'm gonna change this from num1 to answer.
- 00:43 So if there's an answer, if there's not an answer, don't do anything.
- 00:46 If there is an answer, do this.
- 00:47 So that's where we're at so far.
- 00:49 But instead of doing this, let's create some logic in
- 00:53 here that will determine whether the answer is correct or not.
- 00:57 So let's create a variable, and let's call this correct_answer equals.
- 01:03 And let's just take our two original numbers and add them together, okay?
- 01:10 So this is now the correct answer.
- 01:13 Now, we can test our answer against that correct answer and act accordingly.
- 01:17 So let's do another if statement inside of this else statement that we're already in.
- 01:23 We can do that.
- 01:24 We can sort of layer if statements inside of other if statements.
- 01:26 So if, we need parentheses, correct_answer equals,
- 01:33 remember the double equal too, that answer.
- 01:39 Then echo out.
- 01:41 Well, actually we could just echo out this whole thing, right?
- 01:45 But first let's type in Correct!
- 01:50 Concatenate that, okay?
- 01:53 Else, I'm gonna just copy this whole thing and
- 01:59 paste it again, but I'm just gonna change it around here.
- 02:02 I'm gonna say Wrong!
- 02:04 Number one plus number two equals, let's change this to correct_answer.
- 02:12 Concatenate another little message on here, not,
- 02:18 concatenate and then post answer.
- 02:23 Okay, so let's save this.
- 02:26 Did a whole bunch of stuff there, let's reload this and see if it works.
- 02:27 7 + 9 is 52.
- 02:29 Wrong!
- 02:30 7 plus 9 equals 16, not 52.
- 02:33 All right, so 4 + 5 is 9, correct, 4 + 5 = 9.
- 02:40 1 + 6 = 77.
- 02:43 Wrong.
- 02:44 1 + 6 = 7, not 77.
- 02:47 Okay, so it looks like we are good to go, seems to be working.
- 02:50 Now real quick, let's do this new card guy.
- 02:53 Right, now when we click on it, seemed to work on its own.
- 02:58 Why is that?
- 02:59 Because when we submit this form, it's going here but
- 03:03 we haven't actually typed in an answer.
- 03:06 And so when that happens, this page gets reloaded.
- 03:09 But up here, our very first if statement if this answer does not exist right here,
- 03:15 we're not gonna do anything.
- 03:17 So back here nothing is happening.
- 03:19 So 10 + 8 is 1.
- 03:21 Wrong, 10 + 8 = 18, not 1.
- 03:24 New Card, 12, correct.
- 03:27 New Card.
- 03:28 What happens if we type in 12 and I click New Card?
- 03:30 Same thing so I think that's working pretty good.
- 03:33 In the next video, we'll go ahead and finish this up out for
- 03:36 all of our different math functions, subtraction, multiplication, and division.
Lesson notes are only available for subscribers.