PHP开关()无法正常工作

时间:2013-10-31 01:56:05

标签: 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 things而非create things ....当我转到test.php?act=manage时 - 我当然得到{ {1}} ...

那么......我该如何解决这个问题?谢谢

3 个答案:

答案 0 :(得分:7)

php的isset返回一个布尔值。所以$ destination是true还是false,而不是字符串。

尝试

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

答案 1 :(得分:3)

你的问题是:

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

isset会返回truefalse,也不会返回您使用的任何字符串值。

您可以使用以下内容:

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

答案 2 :(得分: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'];
相关问题