我已经测试过它并且它工作正常,但它看起来......奇怪......对我来说.我是否应该担心这是非标准形式,它将在 PHP 的未来版本中删除,或者它可能会停止工作?我总是将默认情况作为最后一种情况,从来没有作为第一种情况...
I've tested this and it works fine, but it looks... weird... to me. Should I be concerned that this is nonstandard form which will be dropped in a future version of PHP, or that it may stop working? I've always had a default case as the final case, never as the first case...
switch($kind)
{
default:
// The kind wasn't valid, set it to the default
$kind = 'kind1';
// and fall through:
case 'kind1':
// Do some stuff for kind 1 here
break;
case 'kind2':
// do some stuff for kind2 here
break;
// [...]
case 'kindn':
// do some stuff for kindn here
break;
}
// some more stuff that uses $kind here...
(如果不太明显,我要做的是确保 $kind 是有效的,因此是默认值:case.但是 switch 也执行一些操作,然后在 switch 之后也使用 $kind.那是为什么默认:落入第一种情况,并设置 $kind)
(In case it's not obvious what I'm trying to do is ensure $kind is valid, hence the default: case. But the switch also performs some operations, and then $kind is used after the switch as well. That's why default: falls through to the first case, and also sets $kind)
建议?这是正常/有效的语法吗?
Suggestions? Is this normal/valid syntax?
这是一个不寻常的习语,当你阅读它时会引起一点停顿,嗯?"的片刻.它有效,但大多数人可能希望在最后找到默认情况:
It is an unusual idiom, it causes a little pause when you're reading it, a moment of "huh?". It works, but most people would probably expect to find the default case at the end:
switch($kind)
{
case 'kind2':
// do some stuff for kind2 here
break;
// [...]
case 'kindn':
// do some stuff for kindn here
break;
case 'kind1':
default:
// Assume kind1
$kind = 'kind1';
break;
}
这篇关于默认为 switch 语句中的第一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!