Control Flow: Switch 3/6
Switch Syntax
A
switch statement is similar to an if / elif/ else statement in that you can check multiple conditions. Here's what it looks like:$myNum = 2;
switch ($myNum) {
case 1:
echo "1";
break;
case 2:
echo "2";
break;
case 3:
echo "3";
break;
default:
echo "None of the above";
}
- A
switchstatement is made up of theswitchkeyword, a variable to check, and a pair of curly braces{ }. Here we check the value of$myNum. - Then we have a
caseblock for each comparison. For examplecase 1: echo "1";break;checks whether $myNum is equal to 1. If yes, itechos "1", and usesbreakto exit theswitchstatement. - Otherwise, the next
caseblock runs. - If all
cases return false, thedefaultcase gets executed.
Instructions
On line 10, there's a
switch statement- Fill the
__spots with the correct code. Check out the example above - Add the
defaultcase.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$fruit = "Apple";
switch ($fruit) {
case "Apple":
echo "Yummy.";
break;
default:
echo "Yummy";
}
?>
</body>
</html>
Comments
Post a Comment