default as first option in switch statement?

前端 未结 9 1797
后悔当初
后悔当初 2020-12-01 23:58

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

9条回答
  •  时光取名叫无心
    2020-12-02 00:11

    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;
    
    }
    

提交回复
热议问题