PHP switch 和 case 逻辑控制

发布于 2024-12-10 17:31:49 字数 246 浏览 1 评论 0原文

可以对案例进行逻辑控制吗?

即:

$v = 0;
$s = 1;

switch($v)
{
    case $s < $v:
        // Do some operation
        break;
    case $s > $v:
        // Do some other operation
        break;
}

有没有办法做类似的事情?

It possible to have logical control on case ?

ie:

$v = 0;
$s = 1;

switch($v)
{
    case $s < $v:
        // Do some operation
        break;
    case $s > $v:
        // Do some other operation
        break;
}

Is there a way to do something similar ?

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

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

发布评论

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

评论(4

千仐 2024-12-17 17:31:49

传递给 switch 的条件是与案例进行比较的值。条件(在你的问题 $v 中)被评估一次,然后 PHP 寻找第一个与结果匹配的情况。

来自手册(在示例#2之后),添加了强调:

switch 语句逐行执行(实际上是逐条语句)。一开始,不执行任何代码。 只有当发现 case 语句的值与 switch 表达式的值匹配时,PHP 才会开始执行这些语句。 PHP 继续执行语句,直到 switch 块结束,或者第一次看到 Break 语句。

在您的问题中, switch ($v) 与您编写的相同:switch (0),因为 $v = 0。然后,您的交换机将尝试找到等于 0 的情况。并且,正如 @Kris 所说

$s < $v 的计算结果为 false,这会在 $v 上触发,因为它在这里为 0。


如果您必须在 case 语句中使用条件,则您的 switch 条件应该是布尔值,例如:

$v = 0;
$s = 1;

switch (true) {
    case ($s < $v):
        echo 's is smaller than v';
    break;

    case ($s > $v):
        echo 's is bigger than v';
    break;
}

现在您的 switch 尝试寻找第一个计算结果为 true 的 case,在本例中为 $s> $v


请注意,虽然 switch-condition 仅评估一次,但每个 case 都会按顺序评估:

$a = 1;

switch ($a) {
    case 2:
        echo 'two';
        break;
    case (++$a > 2):
        break;
    default:
        echo $a;
}

与 default-case 相呼应“2”,因为当将 $a 与“2”进行比较时,$a 为 1 并且该 case 被丢弃; while:

$a = 1;

switch ($a) {
    case (++$a > 2):
        break;
    case 2:
        echo 'two';
        break;

    default:
        echo $a;
}

回显案例“2”中的“two”,因为 ++$a > 2 增加 $a 但不匹配 $a。

default 是一个后备,它的位置并不重要。

注意:上述开关既晦涩又深奥,仅作为证明示例提供。

The condition that is passed to switch is a value the cases are compared against. The condition (in your question $v) is evaluated once and then PHP seeks for the first case that matches the result.

From the manual (after Example #2), emphasis added:

The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement.

In your question switch ($v) is same as if you'd written: switch (0), because $v = 0. Then, your switch will try to find a case which equals to 0. And, just as @Kris said:

$s < $v evaluates to false, which triggers on the $v because it is 0 in here.


If you have to use conditions in case statements, your switch-condition should be a boolean, e.g.:

$v = 0;
$s = 1;

switch (true) {
    case ($s < $v):
        echo 's is smaller than v';
    break;

    case ($s > $v):
        echo 's is bigger than v';
    break;
}

Now your switch tries to seek the first case that evaluates to true, which in this case would be $s > $v.


Note that while the switch-condition is evaluated only once, cases are each evaluated in order:

$a = 1;

switch ($a) {
    case 2:
        echo 'two';
        break;
    case (++$a > 2):
        break;
    default:
        echo $a;
}

echoes '2' from default-case, because when comparing $a to "2" $a was 1 and the case is discarded; while:

$a = 1;

switch ($a) {
    case (++$a > 2):
        break;
    case 2:
        echo 'two';
        break;

    default:
        echo $a;
}

echoes 'two' from case '2' because ++$a > 2 increases $a but doesn't match $a.

default is a fallback and its position doesn't matter.

NB: the aforementioned switches are fugly and esoteric and are only provided as a proof-of-example.

千笙结 2024-12-17 17:31:49

这是行不通的,每种情况都需要是标量值。在你的例子中,情况更糟,它可能看起来有效,但是......

$s < $v 的计算结果为 false,这会在 $v 上触发,因为它在这里为 0。

This will not work, every case needs to be a scalar value. in your example case it is even worse, it may seem to work but...

$s < $v evaluates to false, which triggers on the $v because it is 0 in here.

自由如风 2024-12-17 17:31:49

不是最易读的用法,但是是的,你可以。

如果您确实需要简单的比较,请使用 if/else - if/elseif/else 结构。

Not the most readable use of, but yes you can.

Use if/else - if/elseif/else structure if you do need just a simple comparison.

夏末 2024-12-17 17:31:49

http://php.net/manual/en/control-structs.elseif.php

if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}

http://php.net/manual/en/control-structures.elseif.php

if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文