简体   繁体   中英

PHP page magically returns and echoes “1”

I'm working on a fictional barebones blog to work on my skills , but an unexpected phenomenon is putting me off.

I have a view (no framework), a controller 'index.php' and a layout file. I also have an additional 'functions.php' file to query and connect to the database.

    <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; ?>

This is my layout code .

But when I look at the file ,in the browser ,at the end of the file , a trailing digit "1" is shown.

This magically dissapears when I change the last line to

<?php endforeach; return ' '; ?>

to the foreach line . Why is this coming up?

I'm using Jeffrey Ways 'PHP Fundamentals' Tutorial on Tuts+ Premium and he's not getting this trailing digit.

This is the browser source :

<!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>

The "1" you are seeing is probably the value returned either by echoing successful database query or include function. So when you specify return " " , the default value is overwritten.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM