No matter what behaviour PHP shows, I would find it bad coding if you place
default anywhere but at the bottom, simply because you might run into
unexpected behaviour in other versions of the PHP engine, which I assume
you're experiencing now. You could've seen this coming. When you write a
switch, think of it as the input being matched case by case, with "default"
being the case that matches anything. This means you don't wanna place the
default case anywhere but at the bottom.

Ron


"Benj Carson" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> Hi,
>
> Sorry, I forgot to CC my last reply to the list.  I noticed similar
> behaviour and filed a bug report: 30285.  The case described in the bug
> report is as follows:
>
> $x = "a";
> switch ($x) {
>
> default:
>   echo "default";
>   break;
>
> case "a":
>   echo "a";
>   break;
> }
>
> // Prints "a"
>
> Even though the documentation mentions that default should always be last,
> this is a BC break from 5.0.x and at least 4.3.2.
>
> Thanks,
>
> Benj
>
> On October 7, 2004 10:59 pm, Andi Gutmans wrote:
> > It's always been like that and has been documented for ages in the
> > manual.
> >
> > Andi
> >
> > At 08:24 PM 10/7/2004 -0700, Frank M. Kromann wrote:
> > >Hello Everyone,
> > >
> > >I just discovered a small thing in the switch() statement. The position
> > > of the default: clause has to be at the end of the code:
> > >
> > >$a = 1;
> > >switch ($a) {
> > >    default :
> > >    case 0 :
> > >       $b = 1;
> > >       break;
> > >    case 1 :
> > >       $b = 2;
> > >       break;
> > >}
> > >echo $b; // should print 2 but it prints 1
> > >
> > >$a = 1;
> > >switch ($a) {
> > >    case 1 :
> > >       $b = 2;
> > >       break;
> > >    default :
> > >    case 0 :
> > >       $b = 1;
> > >       break;
> > >}
> > >echo $b; // prints 2 as expected.
> > >
> > >This is tested on Linux with PHP5 CVS-HEAD
> > >
> > >What changed ?
> > >
> > >- Frank
> > >
> > >
> > >
> > >
> > >
> > >--
> > >PHP Internals - PHP Runtime Development Mailing List
> > >To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to