Loading

PHP Switch Statement

Submitted by: 
Language: 
Visitors have accessed this post 4768 times.


Switch Statement is the same as If…Else Statements except that it is easier to use if you have multiple conditions.
Switch Syntax

  1. switch (condition)
  2. {
  3. case condition1:
  4. statement to be executed if n=condition1;
  5. break;
  6. case condition 2:
  7. statement to be executed if n=condition2;
  8. break;
  9. default:
  10. statement to be executed if n is different from both condition 1 and condition 2;
  11. }

condition is a variable expression that is evaluated once. If the first condition is true then the succeeding condition will not be executed anymore, else it will continue until it finds the condition that is equals to true or execute the default statement if no matching condition.
Example

  1. <html>
  2. <body>
  3. <?php
  4. switch ($lastname)
  5. {
  6. case “smith”:
  7. echo "Welcome Mr. Smith";
  8. break;
  9. case “cruz”:
  10. echo "Welcome Mr. Cruz";
  11. break;
  12. default:
  13. echo "Welcome Guest";
  14. }
  15. ?>
  16. </body>
  17. </html>



Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
To prevent automated spam submissions leave this field empty.