2017-07-28 142 views
-5

我做了一個網站,我加了點東西,當我去pm.php它給了我一個錯誤:PHP switch語句錯誤

Fatal error: Switch statements may only contain one default clause in /home/vol10_3/HIDDEN.com/HIDDEN/htdocs/includes/classes/pm.class.php on line 673

代碼:

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"; 
    } 
} 
+1

就像錯誤狀態一樣。如果你還沒有http://php.net/manual/en/control-structures.switch.php –

+0

我已經投票結束這個問題,因爲這個錯誤信息是完全自主的,解釋。 – EJoshuaS

回答

0

你不」實際上使用括號或默認類似的。

<?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的鏈接,其中包含更多有關這些語句如何工作的示例和詳細信息。