default as first option in switch statement?

前端 未结 9 1809
后悔当初
后悔当初 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:29

    I'd personally prefer to do

    switch($kind)
    {
        case 'kind2':
            // do some stuff for kind2 here
            break;
    
        // [...]
    
        case 'kindn':
            // do some stuff for kindn here
            break;
    
        case 'kind1':
        default:
            $kind = 'kind1'; // Redundant if it's already set as 'kind1', but that doesn't make any difference to the code.
            // Do some stuff for kind 1 here
            break;
    
    }
    

提交回复
热议问题