V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
rockyliang
V2EX  ›  程序员

问一个关于 PHP 编码的问题

  •  
  •   rockyliang · 2022-05-22 21:13:04 +08:00 · 1787 次点击
    这是一个创建于 676 天前的主题,其中的信息可能已经有所发展或是发生改变。

    写了一个 test.php 脚本,文件编码为 UTF-8 ,代码如下:

    <?php
    $str = '你好';
    var_dump($str);
    

    在 windows cmd 命令行执行此脚本,正常输出“你好”。

    我的疑问是,windows cmd 命令行的编码是 GBK ,而代码文件的编码是 UTF-8 ,两者的编码不一样,为什么输出中文没有乱码?

    更诡异的是,我将代码文件的编码改为 GBK ,然后再执行,此时命令行、代码文件的编码都是 GBK ,反而乱码了。。。

    我的环境是 Windows 10 ,PHP 7.4

    13 条回复    2022-05-25 19:16:32 +08:00
    hefish
        1
    hefish  
       2022-05-22 21:22:38 +08:00
    说不定是 php7 for win 已经自己处理了这个问题。方便 cmd 下调试。
    ji39
        2
    ji39  
       2022-05-22 21:36:59 +08:00
    你找 Windows 7 试试看, 反过来的
    rockyliang
        3
    rockyliang  
    OP
       2022-05-22 21:53:00 +08:00
    @ji39 卧槽,还真的是! win 10 和 win 7 的 cmd 居然还有这种差异....
    jinliming2
        4
    jinliming2  
       2022-05-22 22:05:26 +08:00
    好像从哪个版本开始,已经默认 unicode 了,打开老版本的迅雷 5 都是全软件除了贴图全部乱码的……
    rockyliang
        5
    rockyliang  
    OP
       2022-05-22 22:16:44 +08:00
    @jinliming2 但我在 cmd 窗口顶部右键 -> 属性 -> 选项 -> 当前代码页,显示的是简体中文 GBK ,如果真的是改为 UTF-8 了,那它这里显示的编码就不正确了
    rockyliang
        6
    rockyliang  
    OP
       2022-05-22 23:16:15 +08:00
    @ji39 @hefish 我又详细测试了一遍,应该不仅仅是系统的问题,还有可能是 PHP 版本的问题。同一个 UTF-8 编码的文件,分别用 5.6.35 、7.0.29 、7.1.16 三个版本的 PHP 去运行:
    在 win7 系统上,三个版本都是乱码
    在 win10 系统上,5.6.35 和 7.0.29 乱码了,7.1.16 没有
    AoEiuV020CN
        7
    AoEiuV020CN  
       2022-05-22 23:22:28 +08:00 via Android
    这种鬼问题也是开发不推荐用 Windows 的原因之一,
    AngryPanda
        8
    AngryPanda  
       2022-05-22 23:32:08 +08:00 via iPhone
    不建议在 Windows 上折腾
    ragnaroks
        9
    ragnaroks  
       2022-05-23 00:31:31 +08:00   ❤️ 1
    如果你是 php.exe test.php 这样执行的,那么实际上是由 php.exe 以 UTF8 读取了 test.php 并执行,这个问题我在 php 5.3 时代遇到过
    encro
        10
    encro  
       2022-05-23 09:14:38 +08:00
    看看,PHP 多友好,哈哈。
    sammeishi
        11
    sammeishi  
       2022-05-23 16:40:00 +08:00
    你迷糊是因为你压根没搞清楚编码的逻辑。CMD 和这个编码无关他只是调用程序。编码涉及到 2 个点 A 文件保存编码 B 读取者什么编码读取。 你保存的 php 文件编码是 UTF8 ,而 php 默认也是 UTF8 读取执行的,那输出正常这有什么问题吗,CMD 在这里也只是个调用者输出也都是 php 给的。 反过来文件是 GBK ,php 还是 UTF8 读取那肯定乱了。你可以在 GBK 的 php 里改一下代码:var_dump(iconv("GBK","UTF-8",$str)) 这就正常了。
    hho2002
        12
    hho2002  
       2022-05-25 19:11:44 +08:00   ❤️ 1
    php-7.1.1 版本修改

    https://github.com/php/php-src/commit/e33ec61f9c1baa73bfe1b03b8c48a824ab2a867e

    https://www.php.net/manual/zh/migration71.windows-support.php

    The console output codepage is adjusted depending on the encoding used in PHP. Depending on the concrete system OEM codepage, the visible output might or might be not correct. For example, in the default cmd.exe and on a system with the OEM codepage 437, outputs in codepages 1251, 1252, 1253 and some others can be shown correctly when using UTF-8. On the same system, chars in codepage like 20932 probably won't be shown correctly. This refers to the particular system rules for codepage, font compatibility and the particular console program used. PHP automatically sets the console codepage according to the encoding rules from php.ini. Using alternative consoles instead of cmd.exe directly might bring better experience in some cases.
    hho2002
        13
    hho2002  
       2022-05-25 19:16:32 +08:00   ❤️ 1
    php.ini 中设置 output_encoding = "GBK" ,执行 GBK 编码文件,windows cmd 输出就正常了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5339 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 08:29 · PVG 16:29 · LAX 01:29 · JFK 04:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.