About us Privacy Disclaimer Contact us
FAQ Help Advertising Feedback
Home Sitemap Search Donate us

  Home > Computer & I'net > switch/case...

   Browse by title articles:
   What is hot:

Numbers - Random

Strings - Connecting

Strings - Pulling apart

if/elseif/else

switch/case

while/do while

Introduction to Visual Basic
No Right Click - Is it really worth it?
Resizing Your Desktop & Color Display
Do-It-Yourself Keyword Optimization
Prev articles123456 7 89101112Next articles



switch/case


Computer & I'net articlesswitch/case

by David Stanley    



switch/case is very similar or an alternative to the if/elseif/else commands. The switch command will test a condition. The outcome of that test will decide which case to execute. Switch is normally used when you are finding an exact (equal) result instead of a greater or less than result.

<?php
switch (condition) {

   case "value1" :
   block of coding;
   if the condition equals value1;
   break;

   case "value2" :
   block of coding;
   if the condition equals value2;
   break;

   default :
   block of coding;
   if the condition does not equal value1 or value2;
   break;

   }
?>

There can be many cases listed. For example purposes, we only show two. There are a few things happening at the same time here :

1. The switch condition is examined and a value is found.
2. The condition value is passed through the cases.
3. If the value matches a case value, the code in that block is executed.
4. If the value does not match any of the case values, the default section is executed.
5. Once a block of coding is finished, the BREAK command is used to jump out of the switch/case area.

<?php
$test = 32;

switch ($test) {

   case "40" :
   echo "The value equals 40.";
   break;

   case "32" :
   echo "The value equals 32.";
   break;

   default :
   echo "The value is not 40 or 32.";
   break;

   }
?>

And the result :
The value equals 32.



-----------------
Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission.





Salary




  Disclaimer | Privacy | Terms of useCopyright © 2004 Nice2know.com