When the browser previews the PHP file the white space at the top affects the layout

  • 2020-05-27 04:29:23
  • OfStack

During the course of writing the PHP file, I noticed that when the PHP file was previewed by the browser, there would be 1 line blank at the top, which affected the layout of the page.

The explanation of BOM header is as follows:

Normally, you use the notepad program that comes with Windows system to write web programs. However, after you write or modify the code of php blog system, you will always encounter the following problems when debugging:

Either cannot log in or cannot log out;
1 blank line appears at the top of the page;
Error warning appears at the top of the page;
Other abnormal conditions.

Analysis reasons:
Since UTF-8 encoding is used, the code is saved as utf-8 encoding after writing or modifying it. While almost all text editing software now displays and edits UTF-8 encoded files, it is a pity that many of them do not perform as well.

When you save a file encoded in UTF-8, you insert three invisible characters at the beginning of the file (_0xEF _0xBB _0xBF, BOM -- Byte Order Mark). It is a string of hidden characters for editors such as notepad to recognize if the file is encoded in UTF-8. For 1-like files, this should not cause much trouble. However, for PHP, PHP is not designed with BOM in mind. It will not ignore the three characters of BOM at the beginning of the UTF-8 file, but will use BOM as part 1 of the text at the beginning of the file. Because of the necessity of < ? or < ? The code after php is executed as PHP, so it will result in the output of the three characters on the page, which will be displayed as a blank line or a garbled code, depending on the browser. Because of the presence of these three characters at the beginning of html1, even if the top padding of the page is set to 0, the entire page will not fit right at the top of the browser. Due to the limitation of the COOKIE send mechanism, COOKIE cannot send out files that already have BOM at the beginning of these files (because PHP sent out the header before COOKIE sent out), so the login and logout functions are disabled. All functions implemented by COOKIE and SESSION are invalid.


When editing or changing any text file, be sure to use an editor that doesn't add BOM. None of the editors under Linux should have this problem. Under WINDOWS, do not use editors such as notepad. The recommended editor is:
Editplus version 2.12 or above;
UltraEdit (need to cancel the 'add BOM' option);
Dreamweaver (need to cancel the 'add BOM' option);
Notepad(need to "convert to UTF-8 without BOM"), etc.

For files that have added BOM, you can save it once with the editor above if you want to cancel it. (Editplus needs to be saved first as gb and then as UTF-8. Dreamweaver can be cancelled in "include Unicode signature (BOM)" in "page properties".

Related articles: