At 6:46 PM -0600 8/31/08, Govinda wrote:
Not that it is an issue, but just to understand the logic-
Why do we have to use 'break' statements in each case?

switch ($i) {
case 0:
    echo "i equals 0";
    break;
case 1:
    echo "i equals 1";
    break;
case 2:
    echo "i equals 2";
    break;
}

all 3 cases fire, even though $i only equals ONE of those case values (if I said that right). I mean if $i==1, then in other languages I don't expect the first or last case to fire! (?) Is the purpose just so one has the OPTION of letting them all fire, and turning that off with 'break'?
Or is there a better reason?

-G


The "break" is to separate each case (i.e., condition)

The switch ($i) isn't even needed if you do it like this:

switch (true)
   {
   case $i==0:
    echo "i equals 0";
    break;

   case $i==1:
    echo "i equals 1";
    break;

   case $i==2:
    echo "i equals 2";
    break;
   }

If you wanted to combine conditions, you could do this:

switch (1)
   {
   case $i==-2:
   case $i==-1:
   case $i==0:

echo "i is less than 0 but greater than -3 and is a counting number (i.e., no fraction)";
    break;

   case $i==1:
    echo "i equals 1";
    break;

   case $i==2:
    echo "i equals 2";
    break;
   }


Typed without checking and after my vacation.

Cheers,

tedd

--
-------
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to