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

关于 php7.1 的一些疑惑 求解答

  •  
  •   yoke123 · 2017-06-02 17:09:21 +08:00 · 2876 次点击
    这是一个创建于 2524 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如图所示:

    为什么结果输出不一样? 求解答

    参考资料:

    Fixed bug #71266 (Missing separation of properties HT in foreach etc)

    https://bugs.php.net/bug.php?id=73792

    11 条回复    2017-06-13 17:57:51 +08:00
    muziyue
        1
    muziyue  
       2017-06-02 17:19:24 +08:00
    $tmp 是 String 啊,有什么问题
    helone
        2
    helone  
       2017-06-02 17:21:28 +08:00   ❤️ 1
    我猜测是 7.0 之前版本有类型隐形转换把,$tmp[$k]的时候自动把$tmp 转换成 array 了,7.1 没有做类型转换,所以输出就是 string 了
    yoke123
        3
    yoke123  
    OP
       2017-06-02 17:25:48 +08:00
    @helone #2 隐式转换吗? 看来是这个问题了 谢谢解答
    shoaly
        4
    shoaly  
       2017-06-02 17:27:29 +08:00
    这个时候 就多么的希望有一个 java 的编译检查, String 对象无法转换成 Array.
    muziyue
        5
    muziyue  
       2017-06-02 17:31:14 +08:00   ❤️ 1
    Applying the empty index operator to a string (e.g. $str[] = $x) throws a fatal error instead of converting silently to array.

    7.1 新特性,现在字符串不会自动转换为数组,跟你发的链接没什么关系
    littleylv
        6
    littleylv  
       2017-06-02 17:32:13 +08:00   ❤️ 1
    参考:
    http://php.net/manual/zh/migration71.incompatible.php#migration71.incompatible.empty-string-index-operator

    字符串不再支持空索引操作符 The empty index operator is not supported for strings anymore ¶
    对字符串使用一个空索引操作符(例如$str[] = $x )将会抛出一个致命错误, 而不是静默地将其转为一个数组。
    yoke123
        7
    yoke123  
    OP
       2017-06-02 17:35:25 +08:00
    @muziyue #5
    @littleylv #6
    懂了 谢谢解答
    Felldeadbird
        8
    Felldeadbird  
       2017-06-02 17:39:24 +08:00
    学习到了。
    sobigfish
        9
    sobigfish  
       2017-06-02 17:39:25 +08:00
    觉得挺好,是时候 brew install homebrew/php/php71 了 ( macOS 本地测试环境)
    ichou
        10
    ichou  
       2017-06-03 19:54:59 +08:00 via iPhone
    看来 7.1 改动还是挺大的,没记错的话,Array 基本就是重写了
    redrain2016
        11
    redrain2016  
       2017-06-13 17:57:51 +08:00
    越来越强类型了!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2432 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:18 · PVG 23:18 · LAX 08:18 · JFK 11:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.