Encoding
Published
Updated
📝
Configure character encoding properly.
Use UTF-8 everywhere. PHP sends UTF-8 by default, static files and the MySQL connection need it set, with utf8mb4 in MySQL.
Use UTF-8 everywhere
Mixed character encodings show up as broken umlauts or � characters. Use UTF-8 in PHP, in static files and in the database.
PHP
PHP sends the charset in the Content-Type header, from the default_charset setting, UTF-8 by default. Set another one per response:
header('Content-Type: text/html; charset=UTF-8');
Static files
Apache delivers static .html and .txt files directly. Set their charset in .htaccess:
AddDefaultCharset UTF-8
MySQL
Use utf8mb4 for tables and for the connection. The older utf8 in MySQL stores only three bytes per character and breaks emoji. Set the connection charset in PHP:
// PDO
$pdo = new PDO('mysql:host=…;dbname=…;charset=utf8mb4', $user, $password);
// mysqli
$mysqli->set_charset('utf8mb4');
Frameworks and CMS set the connection charset in their database configuration.