3

I'm new to web development and I'm really stuck with this silly problem. When I insert php code before html code, something like this: <?php ...some code... ?><!doctype html><html>... it creates extra blank space on top of my page and 'pushes' whole content down. Is it possible to somehow avoid creating that extra space? It also makes extra space if the php code is in body part of html, for example if it's in between two paragraphs it will create extra space between that two paragraphs. Thanks for your answers!

  • Is that code supposed to render something? If yes, it should not be there. If no, you could move it anywhere in the HTML page. So essentially, in both cases you could move the include somewhere else. – amit_g Nov 08 '11 at 03:01
  • Code does **not** magically output spaces. There has to be a statement somewhere that produces output, or there are whitespace characters outside `` blocks. Simply check all files you use. – CodeCaster Nov 18 '11 at 10:05
  • Post `...some code...` or it never happened. – Kevin Peno Nov 22 '11 at 18:37

1 Answers1

0

I guess this what happens

Your code like this

<?php
//your php code here
?>
<!DOCTYPE html PUBLIC "

You should have it like this

<?php
//your php code here
?><!DOCTYPE html PUBLIC "
Quy
  • 29
  • 8
  • It's not rendering anything it's just including some files and checking user session. And there is no extra white space after closing php tag. – user1034842 Nov 08 '11 at 03:08
  • 5
    In any php files you are including that do not produce output, I recommend omitting the trailing `?>`. It is not required to be there, and it stops any trailing whitespace (which is sometimes added by the code editors themselves) from getting into the output. – MrTrick Nov 08 '11 at 03:15
  • @MrTrick Thank you for your solution, I removed `?>` from my php codes and its solved. But I'm confused why closing tag of php `?>` is not necessary in our php files? – Muhammad Saqib Aug 20 '15 at 12:36
  • 1
    @MuhammadSaqib It's not necessary because PHP does not require it. "" and "?>" aren't really like the start and end of an HTML tag, they're more like flags that switch the PHP interpreter on and off. As to *why* it shouldn't go in, see the top answer to http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag – MrTrick Sep 17 '15 at 12:28