2013-10-31 37 views
1

我有這個PHP開關:PHP開關()不工作

<?php 

$destination = isset($_GET['act']); 
switch ($destination) { 


    default: 
     echo "test"; 
     break; 

    case "manage": 
     echo "manage things"; 
     break; 

    case "create": 
     echo "create things"; 

     break; 


} 

?>  

然而,當我去test.php?act=create,輸出manage thingscreate things ....當我去到test.php?act=manage - 當然我得到manage things ...

那麼......我該如何解決這個問題?謝謝

+1

嘗試把你的'默認:'末代替在開始時。 –

+0

@ Fred-ii-那沒用。 – nn2

+1

我的錯誤。那麼這項工作下面有很多好的答案。我嘗試了其中的大部分。 –

回答

7

php的isset返回一個布爾值。所以$ destination是真或假,而不是一個字符串。

嘗試

if(isset($_GET['act'])) 
    $destination = $_GET['act']; 
3

您的問題是:

$destination = isset($_GET['act']); 

isset返回或者truefalse,從來沒有任何正在使用的字符串值。

您可以使用類似:

$destination = isset($_GET['act']) ? $_GET['act'] : ''; 
2

你必須使用:

<?php 

if(isset($_GET['act'])) $destination = $_GET['act']; 

switch ($destination) { 

    case "manage": 
     echo "manage things"; 
     break; 

    case "create": 
     echo "create things"; 
     break; 

    default: 
     echo "test"; 

} 

或者只是使用:

$destination = @$_GET['act'];