In the "PHP Cookbook", they say (p.589) that to properly set the char encoding of outgoing data to utf-8 it is necessary to edit the default_encoding
configuration to utf-8.
However, I cannot find this configuration in php.ini
. Should I simply add a line that would say default_encoding = "utf-8"
?
I do have a ;default_charset = "iso-8859-1"
. As you can see (;
), right now it is not activated. Should I remove the semi-colon and set it to "utf-8"
? Does that take care of the default encoding?
I also found other encoding directives that I don't know what to do about:
[iconv]
;iconv.input_encoding = ISO-8859-1
;iconv.internal_encoding = ISO-8859-1
;iconv.output_encoding = ISO-8859-1
...
; http://php.net/exif.encode-unicode
;exif.encode_unicode = ISO-8859-15
...
;mssql.charset = "ISO-8859-1"
...
;exif.encode_unicode = ISO-8859-15
Is there any reason why I shouldn't simply replace them all with utf-8
?
You should set your default_charset
to UTF-8:
default_charset = "utf-8"
(PHP Cookbook may have a typo in it if they ask you to change the default_encoding
— I've never heard of it.)
You'll also want to make sure that your webserver is set to output UTF-8 if you're going to outputting UTF-8 encoded characters. In Apache this can be set by in the httpd.conf file:
AddDefaultCharset UTF-8
As for modifying the iconv
, exif
, and mssql
encoding settings, you probably don't need to set these (your settings have these commented out anyhow) but it's a good idea to change them all to UTF-8 anyhow.
Modify the line
;default_charset = "iso-8859-1"
to read
default_charset = "utf-8"
About the other options, do not touch them. Avoid default settings, always explicitly set the encoding of in everything you do
- database connections,
- reading and writing files,
- converting with iconv.
Also, beware of the encoding in which your PHP files are saved, make sure that they are in UTF-8, especially if they contain strings to be displayed or compared.
I had a problem on my mysql query that it would not recognize some latin acentuation, so the query would fail. I thought it could be the php file and so on, till i found out that using pdo to call the mysql i had to add the charset. The weird thing is that the previous server i used worked fine!
$dsn = 'mysql:host=localhost;dbname=retirodo_main;charset=utf8';
- - - - - - - - - - - - - - - - - - - - - - - - - ^^^^^^^^^^^^
To resolve I changed "UTF-8" to "UTF-8" (without the dash), solving the problem instead.
CentOS
Izaias Moura
At htaccess level (.htaccess file) the php directive should be php_value default_charset UTF-8
changing from *default_charset = "utf-8"* to *default_charset = "iso-8859-1"* works for me ( i use Apache web server and Linux OS). for some reasons when i use utf-8, some characters don't render well. They either change to some square sign or question mark depending on the web browser used.