Home >Backend Development >PHP Problem >What to do if Chinese characters appear garbled in php cli mode

What to do if Chinese characters appear garbled in php cli mode

王林
王林Original
2021-06-24 11:12:262816browse

The solution to garbled Chinese characters in php cli mode is to execute the command line statement [exec("CHCP 65001");] in the php code and switch the encoding of the command line window so that garbled characters will not appear. .

What to do if Chinese characters appear garbled in php cli mode

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

PHP has always been used to develop web applications, and most developers have never used PHP to develop command line programs. In fact, PHP can not only develop websites and various Web systems, but can also be used to write server scripts, perform scheduled tasks, collection tasks, etc.

When using PHP to develop command line CLI programs, developers using Windows may encounter the problem of outputting Chinese garbled characters.

The reason is that the default encoding of Windows is Ansi (936), and we generally use UTF-8 encoding when writing PHP programs.

Solution:

The first solution:

Execute the chcp 65001 command on the command line;

The second solution Solution:

Use the execution command line statement in PHP to switch the encoding of the command line window. The code is as follows:

exec("CHCP 65001");

Among them, 65001 represents the UTF-8 encoding.

Related video sharing: php video tutorial

The above is the detailed content of What to do if Chinese characters appear garbled in php cli mode. 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