Switch Statement In PHP

Switch Statement In PHP


Posted in : PHP Posted on : January 3, 2011 at 1:06 PM Comments : [ 0 ]

This section contains detail about Switch Statement In PHP.

Switch Statement In PHP

Switch in PHP is also conditional statement but it is slightly different from "if " as it doesn't check condition , it just compare the provided variable or value with each case. If value matched , it will execute the matched case code. If it didn't match to any case, it will execute the 'default' block.

One thing is worth mentioning here, you just need to put 'break' after each case's code, if you want to prevent it from executing other blocks next to the block.

Given below the syntax of the switch :

switch (n)
{
case label1:
code executes if n=label1;
break;
case label2:
code executes if n=label2;
break;
default:
code executes if n is neither label1 nor label2;
} 

Given below example :

<html>
<body>

<?php
$state = "KOLKATA";
switch ($state){
case "UP":
echo "State is UP";
break;
case "MP":
echo "State is MP";
break; 
case "DELHI":
echo "State is DELHI";
break; 
case "KOLKATA":
echo "State is KOLKATA";
break;
case "PUNJAB":
echo "State is PUNJAB";
break;
default:
echo "Not from UP, MP, Punjab, Delhi or Kolkata";
break;
}
?>
</body>
</html> 
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics