PHP Switch Statement
Submitted by admin on Saturday, December 24, 2011 - 11:26.
Switch Statement is the same as If…Else Statements except that it is easier to use if you have multiple conditions.
Switch Syntax
- switch (condition)
- {
- case condition1:
- statement to be executed if n=condition1;
- break;
- case condition 2:
- statement to be executed if n=condition2;
- break;
- default:
- statement to be executed if n is different from both condition 1 and condition 2;
- }
Example
- <html>
- <body>
- <?php
- switch ($lastname)
- {
- case “smith”:
- echo "Welcome Mr. Smith";
- break;
- case “cruz”:
- echo "Welcome Mr. Cruz";
- break;
- default:
- echo "Welcome Guest";
- }
- ?>
- </body>
- </html>
Add new comment
- 145 views