php,开关案件返回未定义的变量

发布于 2025-01-29 15:52:56 字数 922 浏览 0 评论 0原文

hy,

我在函数时得到了开关案例,但是当我调用它时,我会出现错误的变量,而我不知道为什么(我使用php 8)

private function getIDFromBrand() {
    switch ($this->brand) {
        case "Niky":
            $id = 1;
            break;
        case "Pumo":
            $id = 4;
            break;
        case "Coke":
            if ($this->typecoke== 0) {
                $id = 2;
            } else {
                if ($this->typecoke== 1) {
                    $id = 3;
                }
            }
            break;
        case "Tomato":
            $id = 5;
            break;
        case "Riles":
            $id = 6;
            break;
        case "TEST":
            $id = 7;
            break;
    }

    return $id; // Error Undefined variable $id
}

在我的功能顶部声明$ id时,

$ id = null

$ id = 0

开关未更新它,因此它将返回null或0,它将返回声明的值。

Hy,

I got switch case inside function when but when i call it, i got error Undefined Variable and i don't know why (i use PHP 8)

private function getIDFromBrand() {
    switch ($this->brand) {
        case "Niky":
            $id = 1;
            break;
        case "Pumo":
            $id = 4;
            break;
        case "Coke":
            if ($this->typecoke== 0) {
                $id = 2;
            } else {
                if ($this->typecoke== 1) {
                    $id = 3;
                }
            }
            break;
        case "Tomato":
            $id = 5;
            break;
        case "Riles":
            $id = 6;
            break;
        case "TEST":
            $id = 7;
            break;
    }

    return $id; // Error Undefined variable $id
}

When i declare $id at the top of my function, like

$id = null

or

$id = 0

The switch doesn't update it, so it will return null or 0, it will return the declared value.

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

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

发布评论

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

评论(1

雨的味道风的声音 2025-02-05 15:52:56

您的Switch语句没有默认分支,因此,如果$ this-> brand是“堆栈溢出”,则不会运行任何语句,并且代码> $ id 永远不会设置。

请参阅 switch> switch> switch> switch语句的PHP手册:

特殊情况是默认情况案例。此案例与其他情况不匹配的任何内容匹配。

同样,如果$ this-> brand是“可口可乐”,但是$ this- ty- typecoke42,它将与该分支中的任何条件都不匹配。

switch ($this->brand) {
    case "Niky":
        $id = 1;
        break;
    case "Pumo":
        $id = 4;
        break;
    case "Coke":
        if ($this->typecoke== 0) {
            $id = 2;
        } elseif ($this->typecoke== 1) {
            $id = 3;
        } else {
            $id = -1; // WAS PREVIOUSLY NOT SET
        }
        break;
    case "Tomato":
        $id = 5;
        break;
    case "Riles":
        $id = 6;
        break;
    case "TEST":
        $id = 7;
        break;
    default:
        $id = -1; // WAS PREVIOUSLY NOT SET
        break;
}

Your switch statement has no default branch, so if $this->brand is, say, "Stack Overflow", it will not run any of the statements, and $id will never be set.

See the PHP manual for the switch statement:

A special case is the default case. This case matches anything that wasn't matched by the other cases.

Similarly, if $this->brand is "Coke", but $this->typecoke is, say, 42, it will not match either of the conditions in that branch.

switch ($this->brand) {
    case "Niky":
        $id = 1;
        break;
    case "Pumo":
        $id = 4;
        break;
    case "Coke":
        if ($this->typecoke== 0) {
            $id = 2;
        } elseif ($this->typecoke== 1) {
            $id = 3;
        } else {
            $id = -1; // WAS PREVIOUSLY NOT SET
        }
        break;
    case "Tomato":
        $id = 5;
        break;
    case "Riles":
        $id = 6;
        break;
    case "TEST":
        $id = 7;
        break;
    default:
        $id = -1; // WAS PREVIOUSLY NOT SET
        break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文