36

In the "PHP Cookbook", they say (p.589) that to properly set the character 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 file 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 semicolon 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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JDelage
  • 13,036
  • 23
  • 78
  • 112

5 Answers5

53

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 web server 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ben D
  • 14,321
  • 3
  • 45
  • 59
  • For Apache, I don't have an `http.conf` file. I use WAMPserver and they ship Apache with an `httpd.conf` file. I suppose they're the same thing? Also, is that a line you manually add? There's nothing about charset in my `httpd.conf` file. – JDelage Feb 19 '12 at 19:21
  • 1
    that was actually a typo on my part (corrected). If you don't see a reference to `AddDefaultCharset` in the config file, you may want to look through the config included files (it will some something like `Include conf/extra/[something]`) to see if it is set in any of those. If not, just add it to the main httpd.conf file. – Ben D Feb 19 '12 at 19:30
  • Is there some sort of explanation explaining what all those encoding functions do? – CMCDragonkai Jan 07 '14 at 06:37
  • @CMCDragonkai - are you just asking for a general explanation of character encoding, or specifically what the listed settings control? – Ben D Jan 07 '14 at 15:06
  • The listed settings. There's lots from iconv and mb_string. – CMCDragonkai Jan 08 '14 at 17:19
  • no need to use AddDefaultCharset in http.conf. see https://httpd.apache.org/docs/2.4/mod/core.html. meta will override this. – Kiran Jan 02 '18 at 17:02
10

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gioele
  • 9,748
  • 5
  • 55
  • 80
4

I had a problem on my MySQL query that it would not recognize some Latin accentuation, 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 function I had to add the character set. The weird thing is that on the previous server I used it worked fine!

$dsn = 'mysql:host=localhost;dbname=retirodo_main;charset=utf8';
- - - - - - - - - - - - - - - - - - - - - - - - - ^^^^^^^^^^^^
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Miguel
  • 3,349
  • 2
  • 32
  • 28
1

At the htaccess level (.htaccess file), the PHP directive should be php_value default_charset UTF-8.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ontananza
  • 362
  • 6
  • 7
-3

To resolve it, I changed "UTF-8" to "UTF-8" (without the dash), solving the problem instead.

It was on CentOS.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131