Home  >  Article  >  Backend Development  >  How to deal with garbled Chinese characters in php

How to deal with garbled Chinese characters in php

藏色散人
藏色散人Original
2021-03-05 09:46:3723448browse

Solutions to garbled Chinese characters in php: 1. Use "header("content-type:text/html;charset = utf-8");" to solve the problem of garbled characters when there are Chinese characters in the output data; 2. Use "query('SET NAMES UTF8');" to solve the garbled characters.

How to deal with garbled Chinese characters in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

PHP Chinese garbled problem

I mainly used two methods here

The first one

is mainly used to solve echo, prin_r(), var_dump(), when the output data contains Chinese garbled characters question.

<?php
header("content-type:text/html;charset = utf-8");
?>

The second type

is mainly used to solve the problem of garbled characters in the database when there are Chinese characters.

$conn->query(‘SET NAMES UTF8’)
<?php
//连接数据库
$servername = &#39;数据库地址&#39;;
$username = &#39;用户名&#39;;
$password = &#39;密码&#39;;
$dbname = &#39;数据库名称&#39;;
$conn = mysqli_connect($servername,$username,$password,$dbname);
if(!$conn) {
    die(&#39;连接失败:&#39;.$conn->connect_error);
}
echo &#39;连接成功&#39;;
// 防止中文乱码
$conn->query(&#39;SET NAMES UTF8&#39;);

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of How to deal with garbled Chinese characters in php. 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