1

I am designing a website and I have ran into a problem with my HTML. I am executing and echoing content from a PHP file to then display on my page, but my browser is adding a number 1 after the content from the PHP file has been parsed.

Here is my HTML (and PHP):

<section id="header">
<div style="float: left; padding-top: 0px; vertical-align: central;">
<a href="/home"><img src="/img/site/logo.png" alt="DibDibCraft Logo" height="50" width="167" class="logo" /></a>
</div>

<?php echo(include($_SERVER['DOCUMENT_ROOT'] . '/htm/header.php')); ?>
</section>

and here is the content from header.php:

<link rel="stylesheet" type="text/css" href="/style/layout.css" />

<div id="navBarBtnContainer">Hello</div>

The 1 is being placed after the content from header.php is parsed and befoe the end of the section (before the </section> mark). Here is a screenshot:

Number 2 added to HTML page.

As you can see, the number 1 is being added before </section>.

I have checked my code in a HTML and a PHP parser and both have returned no errors that would give a result like this.

I also don't know what the name of this error is (if it is an error), so I haven't been able to find help without asking on this site, so ny apologies if this is a duplicate question, could you please direct me to a question that may help with this problem if this is a duplicate.

Thanks a lot, DibDibs

PS: Could you also tell me why this is happening, this will help me avoid making the same error again in the future, thanks :)

DibDibs
  • 566
  • 4
  • 17
  • 2
    `include` returns true/false (or any `return` value set in the included file) to signify success/failure of the include operation, which you're echoing out. – Marc B Jul 08 '16 at 21:01
  • Yes, it is, sorry, but you don't have to downvote :( – DibDibs Jul 08 '16 at 21:05
  • No worries; I can see how it would have been difficult to find the duplicate question without knowing this was caused by include. I haven't downvoted. – Don't Panic Jul 08 '16 at 21:08
  • Thanks for understanding :) and it must have been someone else then, or the guy that upvoted removed it, sorry about that. – DibDibs Jul 08 '16 at 21:19

3 Answers3

2

problem with

<?php echo(include($_SERVER['DOCUMENT_ROOT'] . '/htm/header.php')); ?>

remove echo in this line

finally output is:

<?php include($_SERVER['DOCUMENT_ROOT'] . '/htm/header.php'); ?>
Naisa purushotham
  • 905
  • 10
  • 18
2
include($_SERVER['DOCUMENT_ROOT'] . '/htm/header.php')

returns Boolean as true == 1 when you echo the previous line of code it returns 1 because it succeed and returned true

delete the echo and it will disappear

Ahmad ghoneim
  • 844
  • 7
  • 13
1

The reason this is happening is because of this code.

<?php echo(include($_SERVER['DOCUMENT_ROOT'] . '/htm/header.php')); ?>

You are actually echoing the returned result of the nested include() statement call, which is 1 (loose true - don't ask me why PHP doesn't use boolean true here).

That would logically be the same as:

// this line generates expect HTML output
$success = include($_SERVER['DOCUMENT_ROOT'] . '/htm/header.php');
// this line generates unexpected 1 output
echo($success);

Since your include actually generates HTML output, your expected HTML is correctly rendered. You then echo the 1 result of the include action, which causes 1 to be displayed.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103