【问题标题】:php switch not functioning as intendedphp 开关未按预期运行
【发布时间】:2020-10-12 02:09:16
【问题描述】:

我正在尝试使用 PHP 的 switch 功能,但我一定是做错了什么。下面的代码将在控制台中回显 $find_top_type 的结果,但似乎没有更进一步。我知道这个查询很好,已经测试并通过 ECHO 验证了它。然而,它似乎并没有深入到 PHP 中。它没有向我显示任何其他 ECHO 请求。

我做错了什么?

$find_top_type = $wpdb->get_var($wpdb->prepare("SELECT column_1 FROM mytable WHERE id = $tid"));
echo $find_top_type;
$find_top_type = 2;
$find_top_type = 4;
$find_top_type = 6;
$find_top_type = 8;

    switch($find_top_type){
        case 2:
            echo "retuning two test";
                break;
        case "4":
            echo "retuning four test";
    }

【问题讨论】:

  • switch前4个赋值语句的作用是什么?
  • 你没有得到任何输出,因为你最后硬编码了find_top_type =8,没有8的情况。尝试先学习基本概念。 php.net/manual/en/control-structures.switch.php
  • @musa 他们正在定义开关,不是吗?这些是条件
  • 如果我理解正确的话 = X 就是它要找的东西?
  • 不,你用等号赋值。

标签: php switch-statement


【解决方案1】:

我认为你应该添加 default 语句和 switch case 的结尾,并且你的 case 4 没有 break 语句。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2021-08-28
    • 2011-10-31
    • 2017-10-23
    • 2013-05-13
    • 1970-01-01
    相关资源
    最近更新 更多