It's important that your entire code has the same charset to avoid issues where characters displays incorrectly.
Here's a little list of things that has to be set to a specific charset.
Headers
Connection
Database
Your database and its tables has to be set to UTF-8. Note that charset is not the same as collation.
You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Caution... There are various different situations that need different ALTERs
. Details Here . Doing the wrong ALTER
is likely to make things worse. -- Rick James
php.ini specification
In your php.ini
file, you should specify the default charset for your platform, like this
default_charset = "utf-8";
(This is in essence the same as doing header('Content-Type: text/html; charset=utf-8');
on all pages)
File-encoding
- It's also important that the
.php
file itself is UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar. You should use UTF-8 w/o BOM.
Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.