The Switch Statement

text zoom

The Switch Statement

We tested two variables in the previous examples. A flag of a country was shown on the screen,depending on the value inside of the variable. A long list of if and else … if statements were used. A better option,if you have only one variable to test,is to use something called a switch statement. To see how switch statements work,check the following example.


<?


$ukFlag ='1';


switch ( $ukFlag )


{
   case '0':
      echo "<IMG SRC=images/usaFlag.jpg>";


      break;


   case '1':
      echo "<IMG SRC=images/usaFlag.jpg>";


      break;


   default:
      echo "No Flag Shown";
}


?>


 


switch ($ukFlag)


You Start with the word 'Switch' then variable name you want to check.


 


case 'What you want to check for':


The word 'case' is used before each value you want to check for. These value are: 0 and 1. These are the values we need after the word 'case'. After the the text or variable you want to check for,a colon is needed ( : ) and code written after this.


 


break;
You need to tell PHP to "Break out" of the switch statement. If you don't,PHP will simply drop down to the next case and check that. Use the word 'break' to get out of the Switch statement.



default:
If nothing case matched than the value that you have under default statement will execute.



                    

Copyright © 2008 VisualBuilder. All rights reserved