Register|Login

Have a question? Click here to ask the community.

Tutorial 14 – Switch Statements

| In: 14. Switch Statements | 7,331 views

Video Tutorial


Video Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
//File Description: PHP Switch Statements
 
/*
$person = "Sarah"; // holds person's name
 
if ($person == "Jack") {
	echo "So bring carrot cake";
}
elseif ($person == "Linda") {
	echo "So bring banana cake";
}
elseif ($person == "Sarah") {
	echo "So bring angel cake";
}
else {
	echo "So bring chocolate cake";
}
*/
 
/*
switch ($variable) {
    case 1:
        do this
        break;
    case 2:
        do this
        break;
    case 3:
        do this
        break;
    default:
        do this
        break;
}
 
*/
 
// Example
 
$person = "Jack"; // holds person's name
 
echo "The party is for $person <br>";
 
switch ($person) {
    case "Jack": // if ($person == "Jack")
        echo "So bring carrot cake";
        break;
    case "Linda":
        echo "So bring banana cake";
        break;
    case "Sarah":
        echo "So bring angel cake";
        break;
    default:
        echo "So bring chocolate cake";
        break;
}
?>

Comment Form

You must be logged in to post a comment.