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;
case label2:
code executes if n=label2;
code executes if n is neither label1 nor label2;

Given below example :


$state = "KOLKATA";
switch ($state){
case "UP":
echo "State is UP";
case "MP":
echo "State is MP";
case "DELHI":
echo "State is DELHI";
case "KOLKATA":
echo "State is KOLKATA";
case "PUNJAB":
echo "State is PUNJAB";
echo "Not from UP, MP, Punjab, Delhi or Kolkata";
Go to Topic «PreviousHomeNext»

Your Comment:

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

Tutorial Topics