PHP 将 <Head> 个文件添加到 <Body>

PHP adding <Head> files into <Body>

我是 PHP 的新手,我的主要问题是,虽然 运行 使用 MAMP 的本地服务器网站页面工作正常,但是当托管在远程仓库上时 header.php 的内容被塞进 <body> 而不是 <head>。我的代码如下:

  <?php
  $pageName = "This is my page title";
  $isHomeStyle = true;
  $mainjs = true; 
  $flexisel = false;
  $lazyload = false; 

  include("includes/header.php");
  include("includes/navbar.php");?>

<body>
...
</body>

当我在本地主机上加载它时,头部出现在它应该出现的位置并且一切正常,但是当我在网上看到它时,代码看起来像这样;

<head></head>
<body>
    <div class="navbar"></div>
    <title>This is my Page title</title>
    <link rel="stylesheet" href="project/css/home.css" />
    <link rel="stylesheet" href="project/css/styles.css" />

...

<body>

显然那是不对的,我错过了什么?

编辑:head.php 文件如下:

<!doctype html>
<html class="no-js" lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Page Name | <?= $pageName ?></title>
    <? if (true == $isHomeStyle): ?>
        <link rel="stylesheet" href="project/css/home.css" />
    <? endif; ?>
  </head>

问题已解决 问题是我使用了 shorthand 标签,显然并非所有地方都完全支持它。谢谢大家!

您没有在 HTML 中关闭 <head>。一些浏览器(肯定是 Chrome)尝试智能地操纵 DOM 来修复语法 HTML 错误。在这种情况下,我相信您的浏览器会自动将包含的输出直接放入 body。在开始 <body> 标签之前添加一个结束 </head> 标签,您应该没问题。另外,我很确定你的 include("includes/navbar.php"); 应该在实际的 body.

根据PHP Pear Docs

Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. This is required for PEAR compliance and is also the most portable way to include PHP code on differing operating systems and setups.

这可能是它在本地主机上运行但在您的在线存储库上不起作用的原因。

此外,确保将 include("includes/navbar.php"); 移到 <body> 标签内。