How to deal with garbled Chinese characters in php

藏色散人
Release: 2023-03-08 13:12:02
Original
23480 people have browsed it

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");
?>
Copy after login

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;);
Copy after login

[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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!