【问题标题】:PHP echoing in wrong location of htmlPHP在html的错误位置回显
【发布时间】:2016-10-14 15:23:43
【问题描述】:

我正在尝试创建一个基于模板的系统来交付内容,但我遇到了一个我似乎无法解决的问题。当我尝试回显包含包含数据的变量时,它会输出到我的 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 的问题吗?任何帮助将不胜感激!

【问题讨论】:

  • 这行不通。以你认为的方式。 $php = 包括“模板/内容/新闻/phpCode.php”;它会立即进行包含。 TBO 我会看看一个合适的 PHP 模板引擎 - twig 真的很好而且很容易上手。
  • 包含在您想要文件的位置。 include 返回真/假,而不是文件的内容。
  • @Richard Housham 有趣的是你提到了树枝,我试过了然后意识到你不能包含 php 代码:(
  • 600 个字符有点棘手,但基本上你有这种布局。 $config(连接,设置到 twig 文件) $logic(获取数据,如果需要则处理) $display(将数据发送到 twig 并让它显示)这就是你得到的分离。我知道开始有点棘手,但相信我,这真的很好。你可以有多个模板,继承的模板。学习真的很好,当你这样做时你会喜欢它。它也是一种相当小的语言,实际上只是在后端制作 PHP,所以你有点知道它在做什么。
  • 你也可以重用代码 sn-ps,很多好东西,它会引导你进入一个好的设计模式

标签: php html include echo


【解决方案1】:

您使用的包含错误。

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

立即输出该文件的输出,并将$php 设置为1(即“它起作用了!”)。

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

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

【讨论】:

  • 旁注:考虑一个合适的框架,如 Laravel,模板引擎将以最佳实践的方式为您处理此类内容。
  • 感谢您的信息,我通过将变量设置为文件路径来使其工作,然后包含而不是与 templateMain.php 中的变量相呼应
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多