utf-8 special characters not displaying

utf-8 special characters not displaying

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why UTF-8 Special Characters Fail on Shared Hosting

Moving a site from a local environment to shared hosting often reveals encoding discrepancies that don't appear locally. The specific scenario described—where index.php renders correctly but profile.php shows question marks, and renaming faq.html to faq.php breaks display—points to a mismatch between file encoding, server configuration, and how the browser interprets the response.

The fact that database tables are UTF-8 is only half the battle. SQL characters are stored as bytes, but those bytes must be correctly transmitted via HTTP and interpreted by the browser. When you see ? or � instead of special characters, it usually means one of three things: the file itself isn't actually UTF-8, the server isn't sending the correct charset header, or there is a specific encoding mismatch in how PHP handles strings versus HTML output.

The inconsistency between .html and .php files is a strong indicator that the issue lies within PHP's handling of the data rather than the raw file storage. If faq.html works but faq.php (containing the same content) fails, the server is likely sending a default character set header for PHP files that conflicts with the actual byte sequence in the file.

Verifying Actual File Encoding

Developers often assume a file is UTF-8 simply because they saved it as such in an editor like VS Code or Sublime Text. While editors use metadata to indicate encoding, the actual bytes on disk tell the real story. If a file was copied from a local machine without proper conversion, or if a text editor introduced a Byte Order Mark (BOM) that wasn't stripped, the server will misinterpret the data.

To verify this, you can use the file command in your terminal:

file /var/www/html/profile.php

If the output says ASCII text, the file is not UTF-8. If it says ISO-8859-1 text, that explains the missing characters. You must ensure the file is actually saved as UTF-8 without a BOM, or configure your server to handle the specific encoding you have on disk.

The Importance of HTTP Headers

Even if the file contains valid UTF--8 bytes, the browser relies on the Content-Type header to know how to decode them. PHP does not automatically send this header; it defaults to application/x-httpd-php without a charset specification. When a charset is missing in the response header, browsers often guess based on the meta tags or previous history, which can lead to inconsistent rendering.

You should explicitly set the charset in your PHP files:

<?php
header('Content-Type: text/html; charset=utf-8');
?>

Place this at the very top of every file before any HTML output. If faq.php fails but faq.html works, it's likely because the .html file contains a <meta charset="utf-8"> tag which forces the browser to interpret the stream correctly, whereas the PHP file was sending an untagged response that fell back to a legacy encoding like ISO-8859-1.

Database and Application Layer Mismatch

The database tables being UTF-8 is necessary but not sufficient. The connection between the application and the database must also agree on the character set. If your PHP code fetches data using mysqli or PDO without specifying the charset, the default collation might differ from what you expect.

Consider this common pitfall:

// Without explicit charset
$sql = "SELECT * FROM profiles";
$result = $conn->query($sql);
while ($row = $result->fetch()) {
    echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8');
}

This code might still fail if the database connection itself hasn't been configured to use UTF-8. You should explicitly set the client character set:

$conn->set_charset("utf8mb4");

If you are using a framework like Laravel, ensure your .env file specifies DB_CHARSET=utf8mb4 and that your migrations define columns as string rather than relying on default types that might inherit legacy collations. For more robust handling of internationalization in PHP applications, resources such as https://laravelcompany.com provide detailed guides on configuring Laravel projects to handle multi-byte character sets correctly from the start.

The jQuery Tab Quirk

The issue with one tab displaying correctly and another not is a classic symptom of mixed encoding within a single page. If one tab loads content via AJAX that was saved with a different encoding, or if the server returns different headers for different endpoints accessed by those tabs, the browser will render them differently. Ensure that any asynchronous requests also include the correct Accept-Charset header and that the response from your backend is consistently UTF-8 encoded regardless of the endpoint path.

Our network