PHP在错误的html位置回显


PHP echoing in wrong location of html

我正在尝试制作一个基于模板的系统来提供内容,但我遇到了一个问题,我似乎无法解决。当我尝试回显包含数据的变量时,它会输出在html的错误部分。

下面是'newstuff.php',这是我要在浏览器上执行的页面,违规变量是$php $head $content

<?php
$php = include "templates/content/newstuff/phpCode.php";
$head = include "templates/content/newstuff/head.html";
$content = include "templates/content/newstuff/content.php";
include realpath(dirname(__FILE__)).'/templates/templateMain.php';
?>

下面是'tempalteMain.php'这是我的模板。注意$php $head $content的回显位置。

<?php
echo $php;
?>

<!DOCTYPE html>
<html>
    <head>
        <?php echo $head; ?> 
    </head>
    <body class="body1" onload="inputBlur2()">
        <div class="borderLine"></div>
        <div class="banner"></div>
        <div class="mainContent1" >
            <div class="header1" >
                <div class="headerContainer" >
                    <ul class="navList1">
                        <li><a id = "B0"  href="index.php">New Stuff</a></li>
                        <li><a id = "B1"  href="MainPage.php">Products</a></li>
                        <li><a id = "B2"  href="ProjectsPage.php">Projects</a></li>
                        <li><a id = "B3"  href="AOrdering.php">About Ordering</a></li> 
                        <li><a id = "B4"  href="ContactMe.php">Contact Us</a></li>
                        <li><a id = "B5"  href="FAQPage.php">FAQ</a></li>
                        <li><a id = "B6" href="SCart.php">My Cart</a></li>
                    </ul>
                </div>
            </div>
            <div class="content1"> 
                <?php echo $content; ?>
            </div>
        </div>
    </body>
</html>

下面是'head.html',这提供了由$head PHP变量传递的代码。

<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="/../StylePR.css">
<title>KickUp Electronics</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

下面是'content.php',它提供了$content PHP变量传递的代码。

<p>Welcome to the new stuff page!!!</p>

最后,这是从chrome DOM编辑器输出的页面源代码。注意,来自content.php的信息的位置是错误的,并且有奇怪的'1'回显出来(另外,当查看页面源时,来自head.html的信息被放置在html标记之外)。

    <!DOCTYPE html>
    <html>
<head><meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="/../StylePR.css">
        <title>KickUp Electronics</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body class="body1" onload="inputBlur2()">
<p>Welcome to the new stuff page!!!</p> <!--Wrong Location!-->
1


        1 

        <div class="borderLine"></div>
        <div class="banner"></div>
        <div class="mainContent1">
            <div class="header1">
                <div class="headerContainer">
                    <ul class="navList1">
                        <li><a id="B0" href="index.php">New Stuff</a></li>
                        <li><a id="B1" href="MainPage.php">Products</a></li>
                        <li><a id="B2" href="ProjectsPage.php">Projects</a></li>
                        <li><a id="B3" href="AOrdering.php">About Ordering</a></li> 
                        <li><a id="B4" href="ContactMe.php">Contact Us</a></li>
                        <li><a id="B5" href="FAQPage.php">FAQ</a></li>
                        <li><a id="B6" href="SCart.php">My Cart</a></li>
                    </ul>
                </div>
            </div>
            <div class="content1"> 
                1            </div>

        </div>
</body></html>

我尝试了很多次寻找解决方案,但都无济于事。这是在html完全加载之前执行echo的问题吗?任何帮助将非常感激!

你用错了include

$php = include "templates/content/newstuff/phpCode.php";

立即输出该文件的输出,并将$php设置为1(即"成功了!").

Handling Returns: include在失败时返回FALSE并引发警告。成功的包含,除非被包含的文件覆盖,返回1。——http://php.net/manual/en/function.include.php

您可以使用输出缓冲来捕获输出,但是更好的解决方案可能是将include调用直接移动到templateMain.php中。