How to set UTF-8 encoding for a PHP file How to set UTF-8 encoding for a PHP file php php

How to set UTF-8 encoding for a PHP file


header('Content-type: text/plain; charset=utf-8');


Also note that setting a header to "text/plain" will result in all html and php (in part) printing the characters on the screen as TEXT, not as HTML. So be aware of possible HTML not parsing when using text type plain.

Using:

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

Can return HTML and PHP as well. Not just text.


PHP, by default, always returns the following header: "Content-Type: text/html" (notice no charset), therefore you must use

<?php header('Content-type: text/plain; charset=utf-8'); ?>