Reputation: 48141
My framework for each pages does the follow:
ini_set('mbstring.internal_encoding','UTF-8');
ini_set('mbstring.func_overload',7);
header('Content-Type: text/html; charset=UTF-8');
Do I need to do a ini_set( 'default_charset', 'UTF-8' );
too?
Upvotes: 11
Views: 69903
Reputation: 1077
Ideally you should set UTF-8 as the default charset at the server configuration level. If your hosting provider is setting another, then you would need to override it with an .ini
setting, or choose another hosting provider. Most set UTF-8 as the default now.
In 2023, you want to be using a universal character set that is capable of all languages, emojis, and whatnot. The only other alternatives for that are UTF-16 and UTF-32, which are more wasteful bytewise for latin or cyrillic text.
The strings you handle in your PHP app may be encoded differently. The .ini
setting won't affect that. You can use mb_detect_encoding
and mb_convert_encoding
if you may recieve text whose encoding is unknown.
Upvotes: 0
Reputation: 81
Please see https://bugs.php.net/bug.php?id=29983 looks to me like some distros still have the problem
test case
echo "ini_get('default_charset') ". ini_get('default_charset')."<br>";
if (!ini_set('default_charset', 'utf-8')) {
echo "could not set default_charset to utf-8<br>";
}
Upvotes: 7
Reputation: 2995
When it comes to the http-header, you're OK as the other answers explain.
But: There are some functions that are default charset aware
From the description of FILTER_SANITIZE_FULL_SPECIAL_CHARS:
Like htmlspecialchars, this filter is aware of the default_charset and if a sequence of bytes is detected that makes up an invalid character in the current character set then the entire string is rejected resulting in a 0-length string.
Upvotes: 6
Reputation: 1249
default_charset ini setting should work for you. PHP always outputs a character encoding by default in the Content-type: header using this setting
Upvotes: 3
Reputation: 3446
No, you don't have to.
header('Content-Type: text/html; charset=UTF-8');
sets this for every page already
Upvotes: 7