Home >Backend Development >PHP Problem >How to solve the problem of Chinese garbled characters in pure PHP code
The solution to the Chinese garbled code in pure PHP code is to add a line of code at the beginning of the PHP file as "header("content-type:text/html;charset=utf-8");".
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
How to solve the problem of Chinese garbled code in pure PHP code?
Chinese garbled problem with pure PHP pages (the data is static)
<?php header("content-type:text/html;charset=utf-8"); //设置编码
Related introduction: The
header() function sends the original HTTP header to the client.
It's important to realize that the header() function must be called before any actual output is sent (in PHP 4 and above, you can use output caching to solve this problem):
<html> <?php // 结果出错 // 在调用 header() 之前已存在输出 header('Location: http://www.example.com/'); ?>
Syntax
header(string,replace,http_response_code)
Parameters
string Required. Specifies the header string to be sent.
replace
Optional. Indicates whether this header replaces the previous header, or adds a second header.
The default is true (replacement). false (allow multiple headers of the same type).
http_response_code Optional. Forces the HTTP response code to the specified value. (Available for PHP 4 and higher)
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to solve the problem of Chinese garbled characters in pure PHP code. For more information, please follow other related articles on the PHP Chinese website!