PHP 包括不适用于索引页

PHP include not working for index page

我正在尝试在我的网站上使用 PHP 包含,但我 运行 遇到了麻烦...

首先这是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Future | Origins</title>
</head>
<body>
    <?php
        if (isset($_GET['p'])) {
            $p = $_GET['p'];

            include('include/header.php');

            switch($p) {

                case "about":
                include('include/about.php');
                break;

                default:
                    $p = "include/main.php";
                break;

            }

            include('include/footer.php');
        }
    ?>
</body>
</html>

如果我的 URL 的样式类似于

,它只会显示我的索引页面的内容
localhost/index.php?p=

我希望它使用默认设置显示我的索引页的内容

localhost

我对 PHP 了解不多,似乎从来没有直接的解决方案。我使用 switch case 在我的网站中包含多个页面。

非常感谢

只需将您的包含移动到 if 语句之外

<body>
<?php
    include('include/header.php');
    if (isset($_GET['p'])) {
        $p = $_GET['p'];



        switch($p) {

            case "about":
            include('include/about.php');
            break;

            default:
                $p = "include/main.php";
            break;

        }
    }
        include('include/footer.php');

?>
</body>