默认作为switch语句中的第一个选项?

default as first option in switch statement?(默认作为switch语句中的第一个选项?)
本文介绍了默认作为switch语句中的第一个选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对此进行了测试,它工作正常,但它看起来……很奇怪……对我来说.我是否应该担心这是非标准形式,将在 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.但是开关也执行一些操作,然后在开关之后也使用 $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语句中的第一个选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)