Saturday, 15 September 2012

PHP Switch Case Statement Syntax And Example

Switch case is an alternative of else-if ladder. If we have multiple conditions to check then we can use switch case statement.

Syntax

switch (n)
{
case label1:
  //code is executed when n = label1
  break;
case label2:
  //code is executed when n = label2
  break;
default:
  //code is executed when n is neither label1 nor label2
}

Example:

<html>
<body>
<?php
$x=1;
switch ($x)
{
case 1:
  echo "Today is Monday";
  break;
case 2:
  echo "Today is Tuesday";
  break;
case 3:
  echo "Today is Wednesday";
  break;

case 4:
  echo "Today is Thursday";
  break;
case 5:
  echo "Today is Friday";
  break;
default:
  echo "This is Weekend";
}

?>
</body>
</html>

No comments:

Post a Comment