为什么我的PHP案例陈述不起作用?

时间:2013-10-14 23:56:18

标签: php

下面是我的代码。挺直前的。只有默认条件正在执行。注释掉其他if语句确实有效。我试图用开关声明替换。

$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. */

非常感谢你提前

1 个答案:

答案 0 :(得分: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