Greek characters encoding works in HTML but not in PHP

encoding, html, mysql, php, utf-8

Solution

Even though it sounds really strange that your mysql data is outputted correctly wheres php strings fail on encoding, the way i would try to solve your problem, would be to break down the issue into steps trying to identify where this miscoding is generated!

First of all, you should try set your `default_charset` to `utf-8` within the php.ini file, which is done like this :

default_charset = "utf-8";

If you can't do that because of provider restrictions you may still be able to change the value at runtime using the ini_set function!

You'll also want to make sure that the webserver is set to output utf-8 encoded files as well! In Apache this can done both in the httpd.conf or using htaccess files :

AddDefaultCharset UTF-8

At this point if everything fails, still... try to go with php headers and relative html charset :

<?php header("content-type: text/html;charset=utf-8") ?>
<!doctype>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <?php echo "α β γ δ ε ϝ ϛ ζ η θ ι κ λ μ ν ξ ο π ϟ ϙ ρ σ τ υ φ χ ψ ω ϡ" ?>
    </body>
</html>

It's very important though, that your files are saved using an appropriate encoding too (utf-8 is almost always the better choice, it helps to prevent issues quite a lot). If you saved files with encoding different than utf-8, destroy them a create new ones out of their old content. Sometimes editors aren't really able to switch encodings properly once the file is created, even though notepad++ generally does well on that; just use `converto to` not the `encode in` feature!

If it is still not working, although i hope it does by now, you may check out some other php alternatives like mb_detect_encoding, mb_convert_encoding, htmlentities and htmlspecialchars in order to fix the problem!

Problem

I have very strange issue which cant figure out. i am using notepad++ and if i save a file as .php with Greek characters ( characters not from database ) it display Greek characters as question marks in web browsers but if i save same characters file as .html it display characters correctly. if Greek characters are displayed from database it show them correctly without issue. Also it is not working correctly on my shared hosting but works fine on my localhost. i tried to save .php file in different encoding but still same issue. I also tried to add php header() and meta tags with utf-8 but no luck. what could be the issue? Thanks

Original source