Home  >  Article  >  Backend Development  >  How to solve the problem of php echo garbled characters

How to solve the problem of php echo garbled characters

藏色散人
藏色散人Original
2020-11-05 13:38:153191browse
php echo is garbled because of encoding problems. The solution is to add a statement such as "header("Content-Type: text/html;charset=gb2312");" in front of php's echo.

How to solve the problem of php echo garbled characters

Recommended: "PHP Video Tutorial"

php echo Chinese garbled problem

The Chinese output by echo is displayed as garbled characters.

In fact, various server scripts should encounter this problem.

It is basically an encoding problem.

Generally speaking Considering encoding compatibility, most pages define the page character set as utf-8

. At this time, in order to display Chinese normally, you need to convert the encoding method, such as

echo iconv("GB2312"," UTF-8",'Chinese'); will not be garbled

There are other methods, such as

Add header("Content-Type: text/html; in front of php's echo charset=gb2312”);

Of course, the simplified Chinese page can also be simply,

Change the UTF-8 in to gb2312

At the same time, attach the PHP query database code Operation

<?php
header("Content-Type:text/html;charset=gb2312");
//设置页面字符集
$conn=mysql_connect("localhost", "root", "****");
//****为mysql密码
mysql_select_db("world");
mysql_query("set names utf8");
$sql="select * from city order by population desc";
$res=mysql_query($sql);
echo "<h1>城市信息一览表</h1>";
echo "<table width=&#39;1000px&#39;>";
echo "<tr>";
echo "<td>id</td><td>name</td><td>countrycode</td><td>district</td><td>population</td>";
echo "</tr>";
while($row=mysql_fetch_assoc($res)){
    echo "<tr>";
    echo "<td>{$row[&#39;ID&#39;]}</td><td>{$row[&#39;Name&#39;]}</td><td>{$row[&#39;CountryCode&#39;]}</td><td>{$row[&#39;District&#39;]}</td><td>{$row[&#39;Population&#39;]}</td>";
    echo "</tr>";
}
echo "</table>";
mysql_close($conn);
?>

How to solve the problem of php echo garbled characters

The above is the detailed content of How to solve the problem of php echo garbled characters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn