1 00:00:00,290 --> 00:00:03,920 Now that you have the skills you need to put together any type of conditional 2 00:00:03,920 --> 00:00:08,340 expression, we're ready to look at another way to structure conditional statements. 3 00:00:09,340 --> 00:00:13,960 PHP provides another control structure called a switch statement. 4 00:00:13,960 --> 00:00:17,320 Switch statements are similar to a series of if statements 5 00:00:17,320 --> 00:00:19,470 that use the same expression. 6 00:00:19,470 --> 00:00:23,230 On many occasions, you may want to compare the same variable or 7 00:00:23,230 --> 00:00:26,480 expression with many different values and 8 00:00:26,480 --> 00:00:31,110 execute a different piece of code depending on which value it equals. 9 00:00:31,110 --> 00:00:34,390 For example you may want to look at the day of the week and 10 00:00:34,390 --> 00:00:38,180 perform different actions based on which day of the week it is. 11 00:00:38,180 --> 00:00:41,470 This is exactly what the switch statement is for. 12 00:00:41,470 --> 00:00:43,100 Let's take a look at this in action. 13 00:00:44,670 --> 00:00:51,990 Let's create a new file named switch.php. 14 00:00:51,990 --> 00:00:56,680 Open and close your PHP tags and we're ready to get started. 15 00:00:57,870 --> 00:00:59,610 We start with the keyword switch. 16 00:01:01,450 --> 00:01:04,110 Then within parentheses, will add the variable or 17 00:01:04,110 --> 00:01:06,930 expression we're going to evaluate. 18 00:01:06,930 --> 00:01:09,150 For our example let's use the day of the week. 19 00:01:10,700 --> 00:01:14,040 We'll use the function date with the lowercase l. 20 00:01:15,150 --> 00:01:19,100 Then within curly braces we're going to add our case statements. 21 00:01:19,100 --> 00:01:25,580 We use the keyword case followed by the value we wish to compare, Monday. 22 00:01:27,600 --> 00:01:30,420 Then we add the action we want to perform. 23 00:01:31,520 --> 00:01:37,640 Echo wash on Monday and 24 00:01:37,640 --> 00:01:42,470 then finally the key word break followed by a semicolon. 25 00:01:42,470 --> 00:01:45,540 This tells the switch statement that we have found a match and 26 00:01:45,540 --> 00:01:46,720 we're finished evaluating. 27 00:01:47,850 --> 00:01:52,750 I'm going to use a list of chores common to pioneer life in 19th century America. 28 00:01:54,130 --> 00:01:56,220 You can find this code in the teacher's notes. 29 00:01:57,500 --> 00:02:00,870 The switch statement also allows us to add default 30 00:02:00,870 --> 00:02:03,228 in case none of the other statements evaluate to true. 31 00:02:03,228 --> 00:02:09,370 We add default colon and then our action. 32 00:02:12,500 --> 00:02:18,020 I do not know what day it is and a break. 33 00:02:19,520 --> 00:02:20,478 Now we can run the script. 34 00:02:20,478 --> 00:02:24,890 PHP space switch.php. 35 00:02:26,000 --> 00:02:29,190 We see mend on Wednesday because today is Wednesday. 36 00:02:30,220 --> 00:02:32,410 Let's move this date to a variable name today. 37 00:02:39,819 --> 00:02:41,960 Now we can do some tests. 38 00:02:41,960 --> 00:02:45,810 First let's set today equal to something that's not a day of the week. 39 00:02:45,810 --> 00:02:48,830 Now let's run our script. 40 00:02:50,190 --> 00:02:51,530 We see a default response. 41 00:02:52,890 --> 00:03:01,610 If we change today to Friday, We now see clean on Friday. 42 00:03:01,610 --> 00:03:06,140 We can also combine value expressions by leaving off the break. 43 00:03:07,400 --> 00:03:12,570 After a case is found, it continues to process statements until a break is found. 44 00:03:12,570 --> 00:03:14,499 If we want to have a single message for 45 00:03:14,499 --> 00:03:18,180 the weekend, we can remove everything between Saturday and Sunday. 46 00:03:24,655 --> 00:03:33,112 Now, when I set today equal to Saturday, We see rest on the weekend. 47 00:03:33,112 --> 00:03:35,670 I can also add an additional statement to Saturday. 48 00:03:41,046 --> 00:03:45,950 Echo Saturday is the weekend. 49 00:03:49,240 --> 00:03:54,000 And now when I run our script, I see both statements. 50 00:03:54,000 --> 00:03:56,830 Saturday is the weekend rest on the weekend. 51 00:03:56,830 --> 00:04:03,380 But if I change to Sunday, I see only the message for the weekend. 52 00:04:05,380 --> 00:04:09,740 The switch statement is a great tool when you want to compare the same variable or 53 00:04:09,740 --> 00:04:12,580 expression with many different values and 54 00:04:12,580 --> 00:04:15,815 execute different pieces of code depending on that value. 55 00:04:15,815 --> 00:04:18,680 While you could use multiple IF statements for 56 00:04:18,680 --> 00:04:23,450 this, the switch statement can give you cleaner and easier to read code. 57 00:04:23,450 --> 00:04:27,920 Once again be clear and expressive in what the code should be doing.