使用PHP文件的头函数? [关闭]

Why would we need to place this at the top of your PHP scripts? Should we only use it if we have an HTML file with some PHP elements in it?

    header('Content-Type: text/html; charset=utf-8');

I have a form built in HTML but the file itself is in PHP.

Some browsers don't just assume the page is html when there is no header, so to get your page to work right in them you have to send a header.

And, obviously, if you are using UTF8 you need to specify that, so that's why you would send this header.

And, of course, headers can be used to specify other types of files will be returned by our php code, like PDF, Excel, CSV, etc.

Versions of PHP prior to 5.6 didn't specify charset=utf-8 by default. This can cause rendering issues if you use non-ASCII characters in UTF-8 and the browser doesn't detect the encoding correctly.