April 4, 2012

PHP Japanese echo string becomes question marks

Question by Asitaka

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:

echo.php:

<?php
function draw(){
echo "日本語";
}
draw();
?>

I get “日本語”

but if I try this :
index.php:

<?php
 some stuff
 include "echo.php";
 draw();
?>

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">
<head>  
<title>Running Projects</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<div id="header">
<? php include "layout.php" ?>
</div>
</body>
</html>

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.

Answer by Stony

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

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

or in php

header('content-type: text/html; charset=utf-8');

sorry for the mistake.

Answer by Starx

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.

HTML 4

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

HTML 5

<meta charset="utf-8" />

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!