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.
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 = '数据库地址'; $username = '用户名'; $password = '密码'; $dbname = '数据库名称'; $conn = mysqli_connect($servername,$username,$password,$dbname); if(!$conn) { die('连接失败:'.$conn->connect_error); } echo '连接成功'; // 防止中文乱码 $conn->query('SET NAMES UTF8');
[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!