【问题标题】:PHP Switch statements Error [closed]PHP Switch语句错误[关闭]
【发布时间】:2017-07-28 02:07:48
【问题描述】:

我创建了一个网站并添加了一个 pm 东西,当我转到 pm.php 时,它给了我一个错误:

致命错误:Switch 语句在第 673 行的 /home/vol10_3/HIDDEN.com/HIDDEN/htdocs/includes/classes/pm.class.php 中可能仅包含一个默认子句

代码:

    function get_new_messages($uid=NULL,$type='pm')
    {
        if(!$uid)
            $uid = userid();
        global $db;
        switch($type)
        {
            case 'pm':
            default:
            {
                $count = $db->count(tbl($this->tbl),"message_id"," message_to LIKE '%#$uid#%' AND message_box='in' AND message_type='pm' AND    message_status='unread'");

            }
            break;

            case 'notification':
            default:
            {
                $count = $db->count(tbl($this->tbl),"message_id"," message_to LIKE '%#$uid#%' AND message_box='in' AND message_type='notification' AND message_status='unread'");
            }
            break;
        }

        if($count>0)
            return $count;
        else
            return "0";
    }
}

【问题讨论】:

标签: php switch-statement


【解决方案1】:

您实际上并没有像那样使用括号或默认值。

<?php

switch ($type) {
    case 'pm':
        // Do something when $type is 'pm'
        // This can be multiple statements
        break;

    case 'notification':
        // Do something when $type is 'notification'
        // This can be multiple statements
        break;

    default:
       // Do something else
       break;
}

您只需添加一次默认值,用于在其他情况均未执行时将执行的代码。 "case: 'pm':" 部分正在启动一个块,该块将持续到 break 语句退出它。如果没有 break 语句,它将进入下一个块(在这种情况下是通知)。

这是指向PHP's website 的链接,其中包含更多示例和有关这些语句如何工作的详细信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2021-04-11
    • 2011-05-23
    相关资源
    最近更新 更多