是否可以使用 ||在 PHP 中切换?

发布于 2024-08-09 04:00:10 字数 247 浏览 7 评论 0原文

switch ($foo)
    {
        case 3 || 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }

上面的代码中,第一个 switch 语句是否有效?如果 $foo 的值为 3 或 5,我希望它调用函数 bar()

switch ($foo)
    {
        case 3 || 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }

In the above code, is the first switch statement valid? I want it to call the function bar() if the value of $foo is either 3 or 5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风轻花落早 2024-08-16 04:00:10

您应该利用 switch 语句的失败:

switch ($foo)
    {
        case 3:
        case 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }

PHP 手册页 有一些这样的例子。

You should take advantage of the fall through of switch statements:

switch ($foo)
    {
        case 3:
        case 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }

The PHP man page has some examples just like this.

水溶 2024-08-16 04:00:10

我认为你需要的是:

switch ($foo)
{
    case 3:
    case 5:
       bar();
    break;

    case 2:
      apple();
    break;
}

有趣的是,我听说 Perl 正在(或者甚至现在)引入了这种语法,大致如下:

if ($a == 3 || 5)

我不是一个粉丝因为我必须编写大量词法解析器,并且相信语言应该尽可能明确,所以我使用了该语法。但是,Perl 之前已经用那些可怕的尾部 ifor 解决了所有这些问题,所以我怀疑它不会有任何问题:- )

I think what you need is:

switch ($foo)
{
    case 3:
    case 5:
       bar();
    break;

    case 2:
      apple();
    break;
}

Interestingly, I've heard that Perl is (or maybe even has, by now) introducing this syntax, something along the lines of:

if ($a == 3 || 5)

I'm not a big fan of that syntax since I've had to write lexical parsers quite a bit and believe languages should be as unambiguous as possible. But then, Perl has solved all these sorts of problems before with those hideous tail-side ifs and ors so I suspect there'll be no trouble with it :-)

攒眉千度 2024-08-16 04:00:10

相反,请使用 switch 语句的主要优点之一:

switch($foo) {
    case 3:
    case 5:
        bar();
        break;

    case 2:
        apple();
        break;
}

Instead, use one of the primary advantages of switch statements:

switch($foo) {
    case 3:
    case 5:
        bar();
        break;

    case 2:
        apple();
        break;
}
南冥有猫 2024-08-16 04:00:10

是的,我认为你所拥有的相当于:

    <?php

    $foo = 5000 ;

    switch( $foo )
    {
      case true :   // Gzipp:  an '=='-style comparison is made
        echo 'first one' ; // between $foo and the value in the case
        break;             // so for values of $foo that are "truthy"
                           // you get this one all the time.

      case 2:
        echo 'second one';
        break;

      default:
        echo 'neither' ;
        break;
    }

    ?>

Yeah, I think what you've got there is equivalent to:

    <?php

    $foo = 5000 ;

    switch( $foo )
    {
      case true :   // Gzipp:  an '=='-style comparison is made
        echo 'first one' ; // between $foo and the value in the case
        break;             // so for values of $foo that are "truthy"
                           // you get this one all the time.

      case 2:
        echo 'second one';
        break;

      default:
        echo 'neither' ;
        break;
    }

    ?>
心清如水 2024-08-16 04:00:10

不,如果你写了 case 3 || 5:,那么你还不如直接写case True:,这肯定不是你想要的。但是,您可以将 case 语句直接放在彼此下面:

switch ($foo)
    {
        case 3:
        case 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }

No, if you wrote case 3 || 5:, then you might as well just write case True:, which is certainly not what you wanted. You can however put case statements directly underneath each other:

switch ($foo)
    {
        case 3:
        case 5:
           bar();
        break;

        case 2:
          apple();
        break;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文