Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to set ini_set( 'default_charset', 'UTF-8' );?

Tags:

php

unicode

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?

like image 849
dynamic Avatar asked Nov 22 '11 15:11

dynamic


People also ask

How to set UTF-8 in PHP?

PHP UTF-8 Encoding – modifications to your php. The first thing you need to do is to modify your php. ini file to use UTF-8 as the default character set: default_charset = "utf-8"; (Note: You can subsequently use phpinfo() to verify that this has been set properly.)

Does PHP use UTF-8?

The utf8_encode() function is an inbuilt function in PHP which is used to encode an ISO-8859-1 string to UTF-8. Unicode has been developed to describe all possible characters of all languages and includes a lot of symbols with one unique number for each symbol/character.

What is UTF-8 PHP?

Definition and Usage The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.


3 Answers

No, you don't have to.

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

sets this for every page already

like image 155
Bart Vangeneugden Avatar answered Oct 18 '22 14:10

Bart Vangeneugden


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>";
}
like image 7
gmgj Avatar answered Oct 18 '22 13:10

gmgj


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.

like image 5
itpastorn Avatar answered Oct 18 '22 14:10

itpastorn