Press "Enter" to skip to content

var_dump(1...9)输出什么?

一个问题, var_dump(1...9)输出什么?

动手验证下:

php -r “var_dump(1...9)”;
string(4) ”10.9“

输出10.9, 乍一看这个var_dump的输出很奇怪是不是? 为什么呢?

这里教大家,如果看到一段PHP代码感觉输出很奇怪,第一反应是看下这段代码生成的opcodes是啥,虽然这个问题其实是词法分析阶段的问题,不过还是用phpdbg分析下吧(一般为了防止opcache的影响,会传递-n):

phpdbg -n -p /tmp/1.php
function name: (null)
L1-35 {main}() /tmp/1.php - 0x7f56d1a63460 + 4 ops
L2 #0 INIT_FCALL<1> 96 "var_dump"
L2 #1 SEND_VAL "10.9" 1
L2 #2 DO_ICALL
L35 #3 RETURN<-1> 1

所以这么看来,早在生成opcode之前,1...9就变成了常量10.9,考虑到这是字面量,我们现在去看看zend_language_scanner.l, 找到这么一行:

DNUM ({LNUM}?"."{LNUM})|({LNUM}"."{LNUM}?)

这个是词法分析定义的浮点数的格式,到这里也就恍然大悟了:
1...9 会被依次接受为: 1. (浮点数1), 然后是 . (字符串连接符号) 然后是.9(浮点数0.9)

所以在编译阶段就会直接生成 “1” . “0.9” -> 字符串的字面量”10.9”

好了,到这里,这个小“谜题”就解释清楚了。

当然这个也不仅仅是PHP会这么定义了,几乎所有的语言都会定义这种缩写浮点数都形式. C语言中有的时候我们为了输入一个浮点型的整形,就可以采用比如 1. 来告诉编译器这是个浮点数.

只不过,一来刚好在PHP中.号还有另外一层含义就是字符串连接,二来...在PHP5.6之后是个新的操作符叫做Splat operator, 可以用来定义可变参数函数,或者解数组,比如,

<?php

function  foo($a, $b, $c) {
        var_dump($a + $b + $c);
}


$parameters = array (1, 2, 3);

foo(...$parameters);
?>

所以, 乍一看才会导致了这个看起来很困惑的结果, 🙂

16 Comments

  1. upload image
    upload image May 3, 2023

    Your email address will not be published. Required fields are marked *

  2. Shriram Poem
    Shriram Poem April 29, 2023

    Shriram The Poem is the ideal choice for modern families looking for a luxurious and fulfilling lifestyle in the heart of Bangalore.

  3. Godrej Splendour
    Godrej Splendour March 21, 2023

    Godrej Splendour is a premium residential project located in the heart of Bangalore’s bustling suburb, Whitefield.

  4. Sobha Neopolis
    Sobha Neopolis March 21, 2023

    Sobha Neopolis is a leading real estate company in Bangalore. Everyone dreams of having their own home and many have helped make their dreams come true. They carefully build each home with an emphasis on quality, practical details, and value for money.They want to gain people’s trust and confidence while launching new products and services.

  5. t rex game
    t rex game January 30, 2021

    I should express that I like this article so a ton and moreover using it in regular. I believe you would continue sharing better than average article here.

  6. c
    c December 22, 2020

    哈哈哈哈哈哈哈哈哈

  7. maple
    maple June 29, 2020

    无力吐槽 (

  8. phper
    phper April 7, 2020

    这样写代码会被看你代码的人骂的,所以还是不要这样写…… ^_^

  9. 阿毛
    阿毛 March 27, 2020

    这样写代码会被打死系列

  10. 沈唁志
    沈唁志 February 25, 2020

    第一个代码块中的引号是中文的🧐

    • laruence
      laruence February 25, 2020

      wordpress特别2 , 自动转

  11. 石樱灯笼
    石樱灯笼 February 25, 2020

    新的PHP面试题(坑)诞生了

    • jaggle
      jaggle July 27, 2020

      这种当面试题,你还是不要坑别人了。不是神仙都做不出来

Comments are closed.