【问题标题】:Having trouble with including PHP file包含 PHP 文件时遇到问题
【发布时间】:2019-03-20 16:56:21
【问题描述】:

我正在尝试将header.php 文件包含在我的index.php 文件中,它们都在同一个文件夹中。但是,它不起作用。

这是我编写 index.php 文件的方法:

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    <link rel="stylesheet" href="css/main.css" />

    <title>Bootstrap</title>
</head>
<body>
    <? php include 'header.php'; ?>
    <p>the header file should be above this line</p>
    <div class="container">
        <div class="row">
            <div class="col-md-4">3</div>
            <div class="col-md-4">2</div>
            <div class="col-md-4">1</div>
        </div>
    </div>

<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="js/jquery.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>  

以下是我编写 header.php 文件的方式:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <p>HEADER INCLUDED</p>
    <p>HIP HIP HORRAH!</p>
</body>
</html>

我希望当我在 Chrome 中打开 index.php 时,我应该会看到类似

的内容
HEADER INCLUDED  
HIP HIP HORRAH!  
the header file should be above this line  
--- the column ---  

但是前两行没有出现。我很困惑我在这里缺少什么。

【问题讨论】:

  • 也许开始标签不应该有像&lt;?php这样的空格而不是&lt;? php
  • 你在开始的 php 标签中留了一个空格。除此之外,生成的 html 文档似乎无效。
  • 哦,是的!成功了!
  • 为什么要在另一个 HTML 文档中包含一个完整的 HTML 文档?
  • 不管文件扩展名如何,它仍然是一个完整的 HTML 文档。当您使包含工作时(通过修复下面提到的错字),您将获得嵌套的 HTML 文档,这不是有效的 HTML。如果你从头文件中删除除了body标签之间的所有内容,你的设置。

标签: php html css bootstrap-4


【解决方案1】:

改变

<? php include 'header.php'; ?>

收件人:

<?php include 'header.php'; ?>

&lt;?php 表示 PHP 代码的开始。但是,&lt;? php 没有任何意义,是一个解析错误。

因为php 不是一种语言结构,例如:forforeachwhile 等...

【讨论】:

  • &lt;? php 不一定是解析错误。如果您没有将 short_open_tags 设置为 On,它应该只作为 HTML 中的字符串输出,因为它以 &amp;lt; 开头,浏览器会将其解释为将被忽略的标记。
  • @AthanasiusKirchner,这是生成的错误:Parse error: syntax error, unexpected 'include' (T_INCLUDE) in /in/bOCXL on line 1
  • 在您的代码示例环境中short_open_tag 设置为开启,请参阅3v4l.org/Z4dOU。如果设置为“Off”,&lt;? php include 'header.php'; ?&gt; 将仅作为字符串输出。这有点猜测,但当他写But the first two lines are not showing up. 时,有些人可能会以这种方式阅读 ganjaam 的描述。这听起来不像是解析错误,但可能是问题不够精确。无论如何,您的修复是正确的,我只是想表明可能存在另一种意外行为。
猜你喜欢
  • 2011-02-06
  • 2016-12-27
  • 1970-01-01
  • 2016-08-22
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-12
相关资源
最近更新 更多