The workshop is designed to help you anticipate possible questions, review what you've learned, and begin putting your knowledge into practice.
1: | How would you use an if statement to print the string "Youth message" to the browser if an integer variable, $age, is between 18 and 35? If $age contains any other value, the string "Generic message" should be printed to the browser. |
2: | How would you extend your code in question 1 to print the string "Child message" if the $age variable is between 1 and 17? |
3: | How would you create a while statement that increments through and prints every odd number between 1 and 49? |
4: | How would you convert the while statement you created in question 3 into a for statement? |
A1: | $age = 22; if (($age >= 18) && ($age <= 35)) { echo "Youth message<br>\n"; } else { echo "Generic message<br>\n"; } |
A2: | $age = 12; if (($age >= 18) && ($age <= 35)) { echo "Youth message<br>\n"; } elseif (($age >= 1) && ($age <= 17)) { echo "Child message<br>\n"; } else { echo "Generic message<br>\n"; } |
A3: | $num = 1; while ($num <= 49) { echo "$num<br>\n"; $num += 2; } |
A4: | for ($num = 1; $num <= 49; $num += 2) { echo "$num<br>\n"; } |