Benj Carson wrote:
> Coming from C (or Java), I find the new behaviour a little strange. If you
I fully agree. Although I wouldn't have used the word 'little' here :-)
> use default to match invalid conditions, putting it at the beginning of a
> switch doesn't seem to be poor practice to me (putting it in the middle
Even putting in the middle makes sense IMHO:
Looking at some of my code I found constructs like
switch ($sort)
{
case 'firstname': ...
default: case 'lastname': ...
case 'email': ...
}
which seems completely legal and understandable to me: I can sort by
different fields (which have a natural order in my data structure so
that's how I list them) and one of them happens to be the default if
$sort is not set. I don't think it's up to the programming language to
judge if such a construct makes sense or not.
> On October 8, 2004 12:10 am, Ron Korving wrote:
>>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.
But the important thing is that PHP4 DOES NOT IMPLEMENT IT THIS WAY!
php -r '$a = 1; switch ($a) { default: print "0\n"; break; case 1: print
"1\n"; }'
prints 1 for PHP 4.3.9 (but prints 0 for 5.1.0-dev)
So a) the documentation does not match the PHP behaviour people got used
to, b) it is different to every other language having a switch statement
I know, c) it would be a nasty BC break to change it now.
I strongly suggest that the behaviour is defined a la C (the position of
default: does not matter) and the documentation is changed accordingly!
There is 1000 times more PHP 4 code around (which may rely on the C-like
behaviour) than PHP 5 code so we still have time. But not much. We have
to resort this as quickly as possible to avoid a big mess...
- Chris