Home Tutorial Php Phpbasics Tutorial PHP Switch Case

 
 

Share on Google+Share on Google+
PHP Switch Case
Posted on: March 29, 2010 at 12:00 AM
Advertisement
In this tutorial we will study about switch case control structure, switch case is used when we need to match a value with every cases, generally we include break statement after every switch case. Examples in this tutorial will make it more clear.

Switch Case Control Structure:

Almost every modern language supports switch case control structure. It is similar to if statements. Switch case is useful when we need to compare a single variable with multiple possible values.

We should know how the switch case works, in switch case every case is considered or checked once and if the condition found true then PHP starts executing the statements until it found a break statement or the end of the switch case block. So, it is always better to put a break statement in case statement.

Example:

<?php

$a=10;

switch($a)

{

case($a%2==0):

echo"Even Number";

break;

case($a%2!==0):

echo "Odd Number";

break;

}

?>

Output:

Even Number

Example:

<?php

$a=12;

switch($a)

{

default:

echo "Default";

break;

case 1:

echo "One";

break;

case 2:

echo "Two";

break;

}

?>

Output:

Default

Example:

<?php

$a=1;

switch($a)

{

case 1:

echo "One";

case 2:

echo "Two";

default:

echo "Default";

}

?>

Output:

OneTwoDefault

Advertisement

Related Tags for PHP Switch Case:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 29, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com