0

下面是我的代码。相当笔直。只有默认条件正在执行。注释掉的 else if 语句确实有效。我试图用 switch 语句替换。

$currCat = (int)$_GET['cPath'];

switch ($currCat) {
case 4:
$tpl_page_body = '/tpl_product_info_display_threads.php';
case 1:
$tpl_page_body = '/tpl_product_info_display_canvas.php';    
case 3:
$tpl_page_body = '/tpl_product_info_display_belts.php';     
case 85:
$tpl_page_body = '/tpl_product_info_display_books.php';
case 75:
$tpl_page_body = '/tpl_product_info_display_beltsDEV.php';
default:
$tpl_page_body = '/tpl_product_info_display.php';
}
/*
if($currCat == 4) {
$tpl_page_body = '/tpl_product_info_display_threads.php';
} elseif ($currCat == 1) {
$tpl_page_body = '/tpl_product_info_display_canvas.php'; 
etc. etc. */

非常感谢你

4

1 回答 1

7

您需要break在每种情况之间放置语句,除非您希望它们级联(我假设您不这样做)

switch ($currCat) {
    case 4 :
        $tpl_page_body = '/tpl_product_info_display_threads.php';
        break;
    case 1 :
        $tpl_page_body = '/tpl_product_info_display_canvas.php'; 
        break;
    // and so on
}

彻底阅读http://php.net/manual/control-structures.switch.php

于 2013-10-14T23:58:49.030 回答