3

I have some files that I need to include in several pages so, I use something like

<html>
<head>
<title>This is a webpage</title>
</head>
<body>
<div>Hello, this is a webpage</div>
<?php include("header.html");?>
<div>Some more text.</div>
<?php include("BlockToInclude.php");?>
<div>Even more text.</div>
<?php include("footer.html");?>
</body>
</html>

The problem is that with each include(); I get a blank space of 20px before the included content. I get around this this by reducing the margin of the first included block by 20px, but I would prefer something that avoid this space instead of compensate it. Also, I'm kind of new to PHP, is this a normal behaviour or should I look for something I'm doing wrong?

UPDATE: I checked in the resulting code and in every spot where I called an insert(); function it appears an stranger character that is not in the original file (nor the caller or the included). I copied the code from the Firefox code viewer (where is not visible) to my editor (RJ TextEd in Windows) where it appears as a question mark in a box; if I save this code as an static html it renders the same, but if I delete those characters then the blank space disappears. Any thoughts?

marlonob
  • 133
  • 1
  • 4
  • 3
    This is an issue with your html/css and has nothing to do with php. Look at the resultant html, that's where your problems are. – Gordon Bailey Aug 13 '12 at 03:12
  • 1
    look in the files you include to see if theres a whitespace at then end just to be sure – Hugo Dozois Aug 13 '12 at 03:17
  • This is urely a issue with your html pages. The content in html pages must be created such that a blank space of 20 px is created, i think you should use firbug type tools to debug. Not at all issue with php include function. – d3vdpro Aug 13 '12 at 03:37
  • I checked in the resulting code and in every spot where I called an `insert();` function it appears an stranger character that is not in the original file (nor the caller or the included). I copied the code from the Firefox code viewer (where is not visible) to my editor (RJ TextEd in Windows) where it appears as a question mark in a box; if I save this code as an static html it renders the same, but if I delete those characters then the blank space disappears. Any thoughts? – marlonob Aug 13 '12 at 03:54

8 Answers8

9

I found this topic here

Someone sais:

I've had this problem before. Make sure you upload your file in ASCII not Binary. Sometimes, with some FTP servers, you have to set this manually. Also try converting your newlines to "\n" in your text editor. Lemme know which editor you're using and I'll give you more specific instructions.

http://forums.digitalpoint.com/showthread.php?t=957679

And someone solved it : I solve this problem using notepad++ (http://notepad-plus.sourceforge.net/) Open php files with notepad++ and in Format menu chose encode in utf-8 without BOM and save it.

Hope this helps to

themhz
  • 8,335
  • 21
  • 84
  • 109
  • Yes, that was the problem. Thanks. I don't thought that were a codification problem because every file had the same. I wonder if some `` could avoid that. – marlonob Aug 13 '12 at 04:12
  • hmm don't know but I think that if you have the right encode you will solve your issue. – themhz Aug 13 '12 at 04:13
  • For those using Adobe Dreamweaver if you "Save As" your page and overwrite it with the "Include Unicode Signature (BOM)" UNCHECKED it fixes it. – Paul Oct 16 '15 at 18:57
1

Do you have any whitespace at the end of the included files? By default, PHP will not create any whitespace from an include directive, unless you have whitespace after it, e.g.

no whitespace:

<?php include('x'); ?>
</div>

will include one line of whitespace:

<?php include('x'); ?>

</div>

If the included files have whitespace at the end, that will get output.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    As far as I can tell he's asking about the layout of the page as rendered by a browser, whitespace in the html shouldn't affect that at all. – Gordon Bailey Aug 13 '12 at 03:15
  • whitespace in text can throw off layouts, especially on pixel-perfect stuff. – Marc B Aug 13 '12 at 03:22
1

Not quite the same, but similar:

I had a mysterious '1' appearing in my html at the point of my include files.

It was a simple and stupid mistake, I put the include in using a short tag like:

<?=include('social-media-box.php')?>

instead of properly:

<?php include('social-media-box.php'); ?>

and this caused the '1' to appear, for whatever reason. Anyhow, using the full php tag got rid of it.

Hope it helps someone.

dmgig
  • 4,400
  • 5
  • 36
  • 47
1

I answered a similar situation here: https://stackoverflow.com/a/26658129/292060

The issue was UTF-8 encoding with BOM characters, that were causing the blank space.

Community
  • 1
  • 1
goodeye
  • 2,389
  • 6
  • 35
  • 68
0

Its html behavior, because you put include under the div tag , try put it beside it.Your code will be like this

...</div><?php include("file.php") ?>
Ahmad Satiri
  • 459
  • 1
  • 7
  • 16
0

This might help you in finding and reducing the white spaces.

  1. Look at the source code and see if you can figure out which included files are creating white spaces.

2.Have your div blocks with border colors so that you know how much space your div blocks are taking.

  1. See if you can have your code of included files without any spaces or extra lines. You can compress the included files
Vijay Kumar
  • 63
  • 1
  • 10
0

Put this rule at the beginning of your CSS, it will disable the default padding and margin for elements in the page.

*
{
    margin: 0;
    padding: 0;
}
avrahamcool
  • 13,888
  • 5
  • 47
  • 58
0

put it before <!DOCTYPE...

like this code:

<?php include "config.php"; ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

and write your page code...

Mohsen
  • 1