php 开关的奇怪行为?

发布于 2024-11-25 22:53:57 字数 574 浏览 3 评论 0原文

我想做的事情的一个例子会更明确:

   var_dump($opti_point); //int 0

    if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

    switch ($opti_point) {

        case ($opti_point>= 0 && $opti_point < 25):
            $test = 0;
            break;

        case ($opti_point >= 25 && $opti_point < 50):
            echo 'we are in this case'; // This case is called !
            $test = 2;
            break;

        default:
            test = 0;
            break;
    }

这里有技巧吗?

谢谢

A sample of what I'm trying to do will be more explicit:

   var_dump($opti_point); //int 0

    if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

    switch ($opti_point) {

        case ($opti_point>= 0 && $opti_point < 25):
            $test = 0;
            break;

        case ($opti_point >= 25 && $opti_point < 50):
            echo 'we are in this case'; // This case is called !
            $test = 2;
            break;

        default:
            test = 0;
            break;
    }

Is there a trick here ?

thx

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

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

发布评论

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

评论(7

迎风吟唱 2024-12-02 22:53:58

我会和你一起验证代码,

   var_dump($opti_point); //int 0 , or false  --- you should use TRUE

    if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

    switch ($opti_point) { // chose the case that is $opti_point (0 or false)

        case ($opti_point>= 0 && $opti_point < 25): // true, so go to next
            $test = 0;
            break;

        case ($opti_point >= 25 && $opti_point < 50): //false si this is the wan I pick
            echo 'we are in this case'; // This case is called !
            $test = 2;
            break; // ingore the rest

        default:
            test = 0;
            break;
    }

你应该在开关中使用 TRUE

I'll go true the code with you

   var_dump($opti_point); //int 0 , or false  --- you should use TRUE

    if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

    switch ($opti_point) { // chose the case that is $opti_point (0 or false)

        case ($opti_point>= 0 && $opti_point < 25): // true, so go to next
            $test = 0;
            break;

        case ($opti_point >= 25 && $opti_point < 50): //false si this is the wan I pick
            echo 'we are in this case'; // This case is called !
            $test = 2;
            break; // ingore the rest

        default:
            test = 0;
            break;
    }

you should use TRUE in the switch

夜司空 2024-12-02 22:53:58

如果这是确切的代码然后尝试这个

var_dump($opti_point); //int 0

if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

switch ($opti_point) {

    case ($opti_point>= 0 && $opti_point < 25):
        $test = 0;
        break;

    case ($opti_point >= 25 && $opti_point < 50):
        echo 'we are in this case'; // This case is called !
        $test = 2;
        break;

    default:
        $test = 0;
        break;
}

if this is the exact code then try this

var_dump($opti_point); //int 0

if ($opti_point>=0 && $opti_point < 25) echo 'good';//echoing good

switch ($opti_point) {

    case ($opti_point>= 0 && $opti_point < 25):
        $test = 0;
        break;

    case ($opti_point >= 25 && $opti_point < 50):
        echo 'we are in this case'; // This case is called !
        $test = 2;
        break;

    default:
        $test = 0;
        break;
}

感情洁癖 2024-12-02 22:53:58

您对 switch-case 的工作原理存在误解。 Case 不会测试您的表达式是否为布尔值 TRUE!

它将其值与“switch”值进行比较!

这是一个解释:

$opti_point>= 0 && $opti_point < 25 求值为 true,整数表示形式为 1,并且由于 PHP 可以自行处理类型,因此它会将 true 变为 1,并将其与 switch 中的值 0 进行比较

$ opti_point >= 25 && $opti_point < 50 计算结果为 false,即 0 作为整数,所以...这就是你的情况;)

You have a misunderstanding on how switch-case works. Case DOES NOT TEST YOUR EXPRESSION TO BE boolean TRUE!

It compares its value to 'switch' value!

Here is an explanation:

$opti_point>= 0 && $opti_point < 25 evalutes to true which integer representation is 1 and since PHP can deal with types on it's own, it turnes true to 1 and compares it with value in switch which is 0

$opti_point >= 25 && $opti_point < 50 evaluates to false which is 0 as integer, so... that's your case ;)

习惯成性 2024-12-02 22:53:57

不幸的是,你不能将比较放在“案例”内......
仅当值可以具有有限数量的值之一时才使用开关,如下所示:

switch ( $val ) {
  case 1:
    echo "Got 1";
    break;
  case 2:
    echo "Got 2";
    break;
  default:
    echo "Got invalid value";
}

解决方法是使用:

switch (true) {
    case ($opti_point>= 0 && $opti_point < 25):
        $test = 0;
        break;

    case ($opti_point >= 25 && $opti_point < 50):
        echo 'we are in this case';
        $test = 2;
        break;

    default:
        test = 0;
        break;
}

哪个可以工作,但有点难看...

此外,您在 < 中缺少单引号code>echo we are in this case'; 应该是 echo 'we are in this case';

你应该使用 if 来代替 =)

You cannot put comparisons inside "case" unfortunately...
A switch is only used when a value can have one of a limited number of values like so:

switch ( $val ) {
  case 1:
    echo "Got 1";
    break;
  case 2:
    echo "Got 2";
    break;
  default:
    echo "Got invalid value";
}

A workaround would be to use:

switch (true) {
    case ($opti_point>= 0 && $opti_point < 25):
        $test = 0;
        break;

    case ($opti_point >= 25 && $opti_point < 50):
        echo 'we are in this case';
        $test = 2;
        break;

    default:
        test = 0;
        break;
}

Which will work, but is a bit ugly...

Also, you're missing a single quote in echo we are in this case'; which should be echo 'we are in this case';

You should be using an if instead =)

一江春梦 2024-12-02 22:53:57

如果您在这种情况下进行比较,则需要将 switch 参数更改为 truefalse

You need to change the switch argument to true of false if you do comparison liek that in the cases.

只为守护你 2024-12-02 22:53:57

你不是在比较你认为你在比较的东西。这是我认为你想要的代码。

var_dump($opti_point); //int 0

if ($opti_point>=0 && $opti_point < 25) {
    $test = 0;
    echo 'You are now here!';
} elseif ($opti_point >= 25 && $opti_point < 50) {
    $test = 2;
} else {
    test = 0;
}

在您的示例中,您正在将逻辑语句的结果...

($opti_point>=0 && $opti_point < 25) // true

与 $opti_point 的值

0 // false

进行比较,因此 PHP 实际上是将您认为的整数转换为布尔值,以将其与条件语句的结果进行比较。

You are not comparing what you think you are comparing. This is the code I think you want.

var_dump($opti_point); //int 0

if ($opti_point>=0 && $opti_point < 25) {
    $test = 0;
    echo 'You are now here!';
} elseif ($opti_point >= 25 && $opti_point < 50) {
    $test = 2;
} else {
    test = 0;
}

In your example, you are comparing the result of the logical statement...

($opti_point>=0 && $opti_point < 25) // true

To the value of $opti_point

0 // false

So PHP is actually converting what you think in an integer into a boolean to compare it with the result of the conditional statement.

我的鱼塘能养鲲 2024-12-02 22:53:57

我认为这是使用 switch 语句的一种非常糟糕的方式,你不应该在 case 中放置条件语句...事实上,我确信这在其他语言中是非法的,并且我不确定它是否应该起作用在 PHP 中。请改用多个串联的 if-else 条件:

if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

I think that's a very bad way to use a switch statement, you should not put conditional sentences in the cases... In fact, I'm sure that that would be illegal in other languages and I'm not sure that it should work in PHP. Use a number of concatenated if-else conditions instead:

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