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 id="城市信息一览表">城市信息一览表</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!
ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PMThe article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and
PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PMThe article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.
PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PMArticle discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.
PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PMThe article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand
PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PMThe article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur
OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PMThe article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.
PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PMThe article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.
PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PMThe article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)







