www.pickatutorial.com Computer Tutorials
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons PHP Switch Statement Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
The Switch statement in PHP behaves almost the same way as it behaves in C++. The switch statement is used to perform one of several different available actions based on one of several different conditions. We use this construct if we need to select and execute one of many different blocks of code. Its syntax follows:

switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed if expression is different from both label1 and label2;
}

This is how it works: • The expression written inside the switch statement i.e. switch(expression) is evaluated. This could also be a variable.
• The value of the expression is compared with the values of each of the case labels.
• If there is a match, the code associated with that case is executed.
• After the code of the match case is executed, break is used to stop the code from running into the next case.
• The default statement is used if a matching case is not found.

Example
<html>
<body>
<?php
$exp = 2;
switch ($exp)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:

echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>




Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.