Home > Article > Backend Development > How to solve the problem of php echo garbled characters
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.
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='1000px'>"; 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['ID']}</td><td>{$row['Name']}</td><td>{$row['CountryCode']}</td><td>{$row['District']}</td><td>{$row['Population']}</td>"; echo "</tr>"; } echo "</table>"; mysql_close($conn); ?>
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!