Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put PHP encoding type header?

I am learning about handling UTF8 character sets and the recommendation is to explicitly set the encoding type in the output headers within your PHP script like so:

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

My question is about where I should place this header. I have a config file that is included into every script and is run first. Should I place it at the top of that file so that this header is included first in every php file?

Will that affect my ability to set other headers, including a header location redirect down the line? Or should I place this just before any html output, such as the rendering of my template file? Ie- can this header be in place before ALL other php processing, and what are the consequences of doing that? Does it affect anything server side, or just the encoding of the output?

Thanks.!

like image 772
Sherri Avatar asked Mar 22 '11 12:03

Sherri


1 Answers

Although early setting of this header will not affect anything server-side nor other types of headers, your question being asked out of assumption that the only content your code going to output is of html type.

However, general PHP application sometimes outputs content of some other types, like application/pdf, application/excel or whatever.

Thus, sending it just before html output seems to be exactly the right place.

like image 174
Your Common Sense Avatar answered Oct 03 '22 16:10

Your Common Sense