Monday, January 26, 2009

How to use switch statement in Php?

Very nice example for switch statement.

If have many blocks and if you want to select one of you blocks actually you can use Switch statement.

The switch statement is used to shorting the code i mean the block of code instead using if..else..elseif.



Syntax:


The switch statement

switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;

}


Example:

switch ($x)
{
case 1:
echo "Index 1 ";
break;
case 2:
echo "Index 2";
break;
case 3:
echo "Index 3";
break;
default:
echo "No number between 1 and 3";
}

If the value of variable $x is 2 the output is

Output:

Index 2

1 comment:

  1. Howdy! I would like to remark that you sure managed to make a marvelous site. I also wanted to ask you something about this blog. Do you participate in some competitions between online blogs?

    ReplyDelete

 

blogger templates | Make Money Online