简体   繁体   中英

How do I get rid of white spaces at the top of my webpage?

I am developing a website in PHP and one thing is annoying me a lot. There is a lot of white space before beginning of the webpage at <!DOCTYPE html> . Have a look at the screenshot of source code of my webpage and google's home page :

我的网页

谷歌的主页

This is really annoying. I don't know what makes those lines jump down to line-6 when it should be at line-1.

The source code of my webpage is -

<?php
header('Content-Type: text/html');
require_once 'core.php';
$index=new Index();
$index->run('home', array('file.js', 'script.js', 'main.js'), array('style.css'));
?>
<!DOCTYPE html>
<!-- rest of the DOM here -->

I think there must be some echo thing in core.php that is printing some white space but it is not so. There no echo ing before <!DOCTYPE html> .

What is happening? Please help me out.

Remember, there's no such thing as a "php script". There's only files which have PHP code blocks embedded in them. The following file would output 4 blank lines, even though there's absolutely no "echo"/"print" calls in it:

<?php
    // this is just a useless comment
?>



<?php
    /// and another useless comment, 4 lines later
?>

ANY text outside of a <?php ?> bracket pair is considered output by the PHP engine.

There's a good chance this is being caused by white space at the end of PHP files that are being included (assuming 'core.php' includes a number of other files). The simplest way around this (and best practice generally), is to omit the final ?> at the end of PHP files. It is optional anyway.

Another quick thing you could try to diagnose this would be to move your (unnecessary) header() call down a line so it is below the core.php include. If this script is causing browser output you'll then get the standard PHP 'headers sent' error, which will tell you where output started.

Does core.php (or any of the files it includes) have blank lines after the final ?> ?

If so, delete the ?> . It's best to let the final ?> be implied by the end of the file.

Otherwise, just start bisecting your codebase until you can determine what is adding it.

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