普通网友 2016-01-09 17:39
浏览 44
已采纳

包括不起作用

I have created a file called index.php, inside the same folder index.php is there is a folder called "includes". Inside it are two php files head.php and header.php. The problem is that those two files are not being included into index.php. Now, I have searched all over stack overflow and I can't seem to find the answer.

<!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>
  • 写回答

3条回答 默认 最新

  • duandanai6470 2016-01-09 17:56
    关注

    As per my comment to the OP:

    @canthandlehtml question is now, if you have a webserver/PHP installed, if it's properly configured, and how you're accessing this. As http://localhost/file.php or as c://file.php in your web browser? Those are 2 different animals altogether. – Fred -ii-

    As per your comment:

    I am indeed accessing it as c://file.php, I was not aware this might be a problem, forgive for the inadequacy, php is something completely new for me

    You're accessing it as c://file.php instead of http://localhost/file.php which is the reason why your includes don't work.

    A web browser will not parse/execute PHP directives in that manner. It requires it to be running through a host with a web server/PHP installed, running and properly configured.

    Sidenote:

    The code that are to be included should "echo" something in order to "show" something, if that is the intention; to echo HTML etc.

    Seeing <?php include 'includes/header.php'; ?> then I take it you have a form of navigation menu.

    If it contains something like:

    <?php
        $var = "Hello world";
    

    and it is not "echo'd", then it won't show up in your rendered HTML, it needs to be echo'd.

    <?php
        echo $var = "Hello world"; // this is a valid directive
                                   // it both echo's and assigns
    

    It's unsure if <?php include 'includes/head.php'; ?> contains meta's, or CSS to be included etc. and if it does contain <head></head> tags. If not, then you will need to add those if you are including that, or JS that is required to be in head tags, etc.


    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // rest of your code
    

    Sidenote: Displaying errors should only be done in staging, and never production.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算