PHP Japanese echo string becomes question marks

Go To


I'm working on a php site that needs both Japanese and English. In most places I have no trouble getting the Japanese to display correctly, but I can't get the characters to display if I use a function like this:


function draw(){
echo "日本語";

I get "日本語"

but if I try this : index.php:

 some stuff
 include "echo.php";

I get "???". Is there any way for me to get my echo file to send the data to the first file in a way it can read it?

EDIT: The website works, and shows Japanese characters correctly, except when it tries to pull a function from a php file Here's the code:

<html lang="ja">
<title>Running Projects</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<div id="header">
<? php include "layout.php" ?>

Where layout.php is just a file with a list of links, I have this on each page so the links are on every page. For a different part, I have to use functions.php to get some data from a database and write it to the page, so, I tried putting layout.php into the functions.php and calling it: The English links appeared, but the Japanese links appeared as question marks.

2012-04-04 07:25
by Asitaka
This should work just fine. How are you viewing the result - deceze 2012-04-04 07:28


You should change your file encoding to UTF-8 and set the header on the website to UTF-8.

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

in HTML5 you should use:

<meta charset="utf-8" /> 

or in php

header('content-type: text/html; charset=utf-8');
2012-04-04 07:29
by René Höhle
My page already has this tag, the Japanese characters work when they're drawn from they don't work if they're drawn from a function Asitaka 2012-04-04 10:52
Then check the file encoding and change it to UTF- - René Höhle 2012-04-04 11:46
perhaps try "echo utf8_encode("日本語"); - René Höhle 2012-04-04 12:33
@Stony utf8_encode converts from Latin-1 to UTF-8. The string "日本語" cannot be encoded in Latin-1. Hence utf8_encode is of no use - deceze 2012-04-04 23:53


You problem definitely has something to do with the character encoding. Try to check the following:

  • All your strings in all PHP scripts are Unicode (UTF-8 is a very common choice). Use utf8_encode() and/or utf8_decode() to force UTF-8 on your strings where necessary.
  • Your server sends PHP output as Unicode (UTF-8 and preferably, but not necessarily, gzipped data)
  • Your browser understands and accepts Unicode (UTF-8). Typically browser would send Accept-Charset: UTF-8,*;q=0.5 in the GET request to hint it's Unicode capability.

Finally, I have checked your code with PHP version 5.3.6 and Firefox, Chrome and IE 9. Firefox and Chrome prints the Japanese characters as expected. It's only IE 9 which doesn't print it correctly. Snooping on the GET request from IE reveals, it is indeed not sending any Accept-Charset: UTF-8,*;q=0.5. I am not entirely sure how to force IE to send that because in my case, clearly my server (Apache) together with PHP was definitely sending UTF-8 string back.

One more hint - you may try converting your strings into HTML entities. For example - echo "&#x00A9;"; would print ©. But, I am not 100% sure how to convert all strings into HTML entities using PHP. I have unsuccessfully attempted with htmlentities() and htmlspecialchars(). But, it didn't change anything for IE.

2012-04-04 08:55
by gsbabil
You're right, I was having trouble with the html entities, some how the string was getting messed up, when I echoed just the text, it works, but in an html string i - Asitaka 2012-04-04 11:59
You can't "force UTF-8" on your strings using utf8_encode. A string is either UTF-8 encoded or it is in some other encoding. If the latter, you can convert it from that encoding to UTF-8. utf8_encode converts from Latin-1 to UTF-8. You cannot represent the text "日本語" in Latin-1. So utf8_encode is pointless here, as it pretty much always is - deceze 2012-04-04 23:51


i have same problem. But i handle it.

convert "echo.php"'s encoding to EUC-JP or SHIFT-JIS.

function draw(){
echo mb_convert_encoding("日本語", 'UTF-8', array('EUC-JP', 'SHIFT-JIS', 'AUTO'));

This works for me :)

2013-07-03 03:58
by bayaraa


That happens when charset is not defined or is incorrect.

You can use meta tags to define the charsets. Place the following meta tags as needed on the head section of the page, and It will be rendered correctly.


<meta http-equiv="content-type" content="text/html; charset=UTF-8">


<meta charset="utf-8" />
2012-04-04 07:38
by Starx


add below meta tags to header

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta http-equiv="Content-Language" content="ja" />
2012-04-04 07:36
by Hassy31


MySQL and PHP need to be handling the same character set.

Try adding ..


.. after connecting to your database. Always works for me!

2014-05-29 00:55
by Butsuri
This would be important if I were having problems with the database, but I was having trouble passing Japanese between php classes - Asitaka 2014-06-01 21:30


If you are using php and mssql you will also need to specify characterset in the mssql connection string.

Like this:

$connectionInfo = array("Database"=>"db name", "UID"=>"username", "PWD"=>"pw","CharacterSet" => "UTF-8");
$serverName = "server";
$conn = sqlsrv_connect($serverName, $connectionInfo);

Please note you still need to do the things suggested above declare your html charset like this HTML 5:

<meta charset="utf-8" />

php charset like this:

header('content-type: text/html; charset=utf-8');
2018-03-30 16:50
by cocopan