包括不起作用

can*_*tml 1 html php include

我创建了一个名为index.php的文件,在同一文件夹index.php中有一个名为"includes"的文件夹.里面是两个php文件head.php和header.php.问题是这两个文件没有包含在index.php中.现在,我已经搜索了所有堆栈溢出,我似乎无法找到答案.

<!doctype html>
<html>
    <?php include 'includes/head.php'; ?>

    <body>
        <?php include 'includes/header.php'; ?>
        <div id="container">
            <aside>
                <div class="widget">
                    <h2>Widget Header</h2>
                    <div class="inner">
                        Widget contents
                    </div>
                </div>
            </aside>
        </div>
    </body>

</html>
Run Code Online (Sandbox Code Playgroud)

Fun*_*ner 5

根据我对OP的评论:

@canthandlehtml问题现在​​是,如果您安装了Web服务器/ PHP,是否已正确配置,以及您如何访问它.作为http://localhost/file.php或作为c://file.php in您的网络浏览器?这些是两种不同的动物. - 弗雷德-ii-

根据你的评论:

我确实以c://file.php的形式访问它,我不知道这可能是一个问题,原谅不足之处,php对我来说是全新的

您正在访问它,c://file.php而不是http://localhost/file.php您的包含不起作用的原因.

Web浏览器不会以这种方式解析/执行PHP指令.它要求它通过安装了Web服务器/ PHP的主机运行,运行并正确配置.

边注:

如果这是意图,那么要包含的代码应该"回应"某些东西以"显示"某些东西; 回应HTML等

看到<?php include 'includes/header.php'; ?>那时我认为你有一种导航菜单.

如果它包含以下内容:

<?php
    $var = "Hello world";
Run Code Online (Sandbox Code Playgroud)

并且它不是"echo'd",然后它不会显示在您呈现的HTML中,它需要回显.

<?php
    echo $var = "Hello world"; // this is a valid directive
                               // it both echo's and assigns
Run Code Online (Sandbox Code Playgroud)

不确定是否<?php include 'includes/head.php'; ?>包含meta,或包含CSS等,如果它包含<head></head>标签.如果没有,那么你需要添加那些,如果你包括那个,或者需要在头标签中的JS等.


错误报告添加到文件的顶部,这将有助于查找错误.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code
Run Code Online (Sandbox Code Playgroud)

旁注:显示错误只能在分段中完成,而不能在生产中完成.