【问题标题】:PHP page magically returns and echoes "1"PHP页面神奇地返回并回显“1”
【发布时间】:2014-04-03 04:39:09
【问题描述】:

我正在编写一个虚构的准系统博客来提高我的技能,但一个意想不到的现象让我望而却步。

我有一个视图(无框架)、一个控制器“index.php”和一个布局文件。 我还有一个额外的“functions.php”文件来查询和连接到数据库。

    <h1>The Blog</h1>

    <?php foreach($posts as $post) : ?>

    <article>
        <h2> 
            <a href="single.php"><?= $post['title']; ?> </a>
        </h2>
        <div class="body"><?= $post['body'] ?> </div>
    </article>

    <?php endforeach; ?>

这是我的布局代码。

但是当我在浏览器中查看文件时,在文件末尾显示了一个尾随数字“1”。

当我将最后一行更改为时,这会神奇地消失

<?php endforeach; return ' '; ?>

到 foreach 行。 为什么会出现这种情况?

我在 Tuts+ Premium 上使用 Jeffrey Ways 'PHP Fundamentals' 教程,但他没有得到这个尾数。

这是浏览器源代码:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>


        <h1>The Blog</h1>


        <article>
            <h2> 
                <a href="single.php">My first Post </a>
            </h2>
            <div class="body">Here is the body of the first post </div>
        </article>


        <article>
            <h2> 
                <a href="single.php">PHP isn't too hard </a>
            </h2>
            <div class="body">Here is the body for the second post </div>
        </article>


        <article>
            <h2> 
                <a href="single.php">My third Post </a>
            </h2>
            <div class="body">This is not that hard , is it ? </div>
        </article>

        1
</body>
</html>

【问题讨论】:

  • 控制器如何使用这个布局数据?这就是1 的来源。
  • @DaveChen。我的 php.ini 中有一个“auto_append_file =”,但没有任何价值
  • 当我什么都不返回时,它就消失了。
  • @NihalSahu:你如何包含文件?向我们展示该代码。
  • 必须是包含此文件的方式。请记住,PHP 的 include 方法在成功时返回 '1'(人们通常不希望 include 返回一个值)所以 (a) 不小心打印了包含的结果,例如如果包含成功,echo include "foo.php"; 将打印 1,并且 (b) 包含文件中的 return ' '; 将覆盖该返回值,以便与您所看到的相符。

标签: php foreach return


【解决方案1】:

您看到的“1”可能是通过回显成功的数据库查询或include 函数返回的值。所以当你指定return " "时,默认值会被覆盖。

【讨论】:

  • @Du_Kuch ,谢谢,我没有注意到 = 包括:因为这看起来很像
猜你喜欢
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 2015-03-11
  • 2012-08-21
  • 1970-01-01
  • 2011-03-03
  • 2021-10-27
  • 1970-01-01
相关资源
最近更新 更多