Saturday, 24 November 2012

Episode 7: Using switch statements



Episode 7: Using switch statements

For most situations, the if conditional statement works best. However, some times you have a list of conditions and want to execute different statements

The switch statement tests the value of one variable and executes the block of statements for the matching value of the variable. The general format is as follows:

switch ( $variablename )
{
case value :
block of statements;
break;
case value :
block of statements;
break;
...
default:
block of statements;
break;
}

The switch statement tests the value of  $variablename. The script then skips to the case section for that value and executes statements until it reaches a break statement or the end of the switch statement. If there is no case section for the value of $variablename, the script executes the default section. You can use as many case sections as you need. The
default section is optional. If you use a default section, it’s customary to put the default section at the end, but as far as PHP is concerned, it can go anywhere.


Written by “Shojib”

No comments:

Post a Comment