# Practice: if

Multiple choice

What does this output? The URL is `...something.php?age=19`.

1. <?php
2. \$age = \$_GET['age'];
4. if (\$age >= 21) {
6. } A

yes

B

no

C

Can't tell from the information given.

Not graded. So why do it?

Here's code that does the same thing: conditionally sets a variable. Compare the code, and you'll see the difference.

Multiple choice

What does this output? The URL is `...something.php?age=22`

1. <?php
2. \$age = \$_GET['age'];
3. if (\$age >= 21) {
5. }
6. else {
8. } A

yes

B

no

C

Can't tell from the information given.

Not graded. So why do it?

Fill in the blank

Simple if, numeric test

What output does this generate?

1. <?php
2. \$cost = 10;
3. \$happiness = \$cost/5;
4. if (\$cost < 18) {
5.     \$happiness += 3;
6. }
7. print \$happiness;

`+=` means "add to." So if `\$a` has 6 in it, after `\$a += 3;`, `\$a` will be 9. Not graded. So why do it?
Multiple choice

One way to record dates is the number of days since the start of the year. So January 1 would be 1, January 31 would be 31, February 1 would be 32, and so on. For most years, December 31 would be 365.

Here's some code, handling dates that way. Would the code output a happy birthday message?

1. <?php
2. \$currentDayInYearToday = 221;
3. \$currentDayInYearBirthday = 19;
4. print "<p>Day of the year: \$currentDayInYearToday.</p>";
5. if (\$currentDayInYearToday = \$currentDayInYearBirthday) {
6.     print "<p>Happy birthday!</p>";
7. } A

Yes, it would output a happy birthday message.

B

No, it would not output a happy birthday message.

Not graded. So why do it?

Multiple choice

What would this code output? The URL is `...something.php?weight=0`.

1. <?php
2. if (\$weight <= 0) {
3.     \$output = "<p>Sorry, weight must be positive.</p>";
4. }
5. else {
6.     \$shipping = \$weight * 0.5;
7.     \$output = "<p>Shipping: \$shipping</p>";
8. }
9. print \$output; A

The shipping cost.

B

An error message.

C

Can't tell from the information given.

Not graded. So why do it?

Reflect

Write a program that gets an animal's height from the URL. It outputs "That's a tall animal!" if the height is more than 2 (meters). Otherwise it outputs "That's not a tall animal."

If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site. Here's what I wrote.

1. <?php
2. \$height = \$_GET['height'];
3. if (\$height > 2) {
4.   \$message = "That's a tall animal!";
5. }
6. else {
7.   \$message = "That's not a tall animal.";
8. }
9. print "<p>\$message</p>\n";

Nice! Good work!

Fill in the blank

Silly and geeky

What will this code output?

1. <?php
2. \$name = 'joe';
3. if (\$name == 'kieran') {
4.     \$silliness = 6;
5.     \$geekiness = 7;
6. }
7. else {
8.     \$silliness = 2;
9.     \$geekiness = 4;
10. }
11. \$rating = \$silliness + \$geekiness;
12. print \$rating; Not graded. So why do it?
Multiple choice

What will this output? The URL is something like: `...something.php?dish=Corndog`

1. <?php
2. \$dish = \$_GET['dish'];
3. \$isYummy = 'yes';
4. if (\$dish == 'corndog') {
5.     \$isYummy = 'no';
6. }
7. print \$isYummy; A

yes

B

no

C

Can't tell from the information given.

Not graded. So why do it?

Multiple choice

What will this output? From a URL like this: `something.php?ingredient=Curry`

1. <?php
2. \$ingredient = \$_GET['ingredient'];
3. \$ingredient = strtolower(\$ingredient);
4. if (\$ingredient != 'curry') {
6. }
7. else {
9. } A

B

C

Can't tell from the information given.

Not graded. So why do it?

Multiple choice

What would this code output?

1. <?php
2. \$location = 'Australia ';
3. \$location = strtolower(\$location);
4. if (\$location == 'australia') {
5.     \$todo = 'Get some vegemite';
6. }
7. else {
8.     \$todo = 'Go to Australia';
9. }
10. print \$todo; A

Get some vegemite

B

Go to Australia

Not graded. So why do it?

Multiple choice

What would this code output?

1. <?php
2. \$color = 'black ';
3. \$color = strtolower(trim(\$color));
4. if (\$color != 'black') {
5.     \$message = "\$color is the new black";
6. }
7. else {
8.     \$message = "\$color is itself again";
9. }
10. print \$message; A

black is the new black

B

black is itself again

Not graded. So why do it?

Multiple choice

What will this output? The URL is `something.php?pet=cat`

1. <?php
2. \$petType = \$_GET['pet'];
3. if (\$petType == 'dog' || \$petType == 'cat' || \$petType == 'goat') {
4.     \$message = 'Favored pet';
5. }
6. else {
7.     \$message = 'Not a favored pet';
8. }
9. print \$message; A

Favored pet

B

Not a favored pet

C

Can't tell from the information given.

Not graded. So why do it?

Reflect

Explain this code. How would you improve it?

1. <?php
2. \$pieType = \$_GET['pie'];
4. if (\$pieType == 'pecan' || \$pieType == 'custard') {
6. }
7. if (\$numberPiesToBuy == 1) {
8.     \$noun = 'pie';
9. }
10. else {
11.     \$noun = 'pies';
12. }
If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site. Ethan

The first two lines get input from the URL: pie type, and number.

If it's a pecan or custard, add another one.

Output how many pies to buy. Use "pie" if it's one pie, and "pies" if there's more than one.

I'd improve it by trimming the text, and making it lowercase.

Nice! Good job!

Reflect

Explain what this code does.

1. <?php
2. \$order = \$_GET['order_amount'];
3. \$primeMember = \$_GET['prime'];
4. if (\$order > 25 || \$primeMember == 'yes') {
5.     \$shipping = 0;
6. }
7. else {
8.     \$shipping = \$order / 10;
9. }
10. \$orderTotal = \$order + \$shipping;
11. print "<p>Order: \$order<br>Shipping: \$shipping<br>Total: \$orderTotal</p>";
If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site. Georgina

It works out shipping costs for an order. It's free for Prime members, or if the order is for more than \$25.

Got it! Thanks!

Multiple choice

Which of the following are equivalent? That is, they do the same thing for any value of `\$height`.

• 1. if (\$height < 0.1 || \$height > 2) {
• 2. if (\$height > 2 && \$height < 0.2) {
• 3. if (!(\$height >= 0.1 && \$height <= 2)) {
• 4. if (\$height >= 0.1 && \$height <= 2) { A

1 and 2

B

1 and 3

C

1 and 4

D

They're all equivalent.

E

Can't tell from the information given.

Not graded. So why do it?

Reflect

Explain what this code does.

1. <?php
2. \$animal = \$_GET['animal'];
3. if (\$animal == 'giraffe') {
5. }
6. else if (\$animal == 'lion') {
7.     \$faveGame = 'Tag';
8. }
9. else if (\$animal == 'tiger') {
10.     \$faveGame = 'Operation';
11. }
12. else if (\$animal == 'penguin') {
13.     \$faveGame = "Don't Break the Ice";
14. }
15. else {
16.     \$faveGame = '(Unknown)';
17. }
18. print "<p>A {\$animal}'s fave game is \$faveGame.</p>";
If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site. Ray

It tells you what an animal's favorite game is. It only knows a few animals. It says unknown if it doesn't know the animal.

Good! That's what it does.

Reflect

Explain what this code does.

1. <?php
2. \$team1Score = \$_GET['team1'];
3. \$team2Score = \$_GET['team2'];
4. \$spread = abs(\$team1Score - \$team2Score);
5. if (\$spread == 0) {
6.     \$comment = 'A tie!';
7. }
8. else if (\$spread < 3) {
9.     \$comment = 'A close game';
10. }
11. else if (\$spread < 8) {
12.     \$comment = 'A comfortable win';
13. }
14. else {
15.     \$comment = 'A thrashing!';
16. }
17. print "<p>\$comment</p>";
If you were logged in as a student, the lesson would pause here, and you'd be asked to type in a response. If you want to try that out, ask for an account on this site. 