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