- 本文地址: https://www.laruence.com/2020/02/23/1990.html
- 转载请注明出处
一个问题, 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); ?>
所以, 乍一看才会导致了这个看起来很困惑的结果, 🙂
Your email address will not be published. Required fields are marked *
Shriram The Poem is the ideal choice for modern families looking for a luxurious and fulfilling lifestyle in the heart of Bangalore.
Thanks for this amazing article!
Thanks for this beautiful blog!
Godrej Splendour is a premium residential project located in the heart of Bangalore’s bustling suburb, Whitefield.
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.
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.
哈哈哈哈哈哈哈哈哈
无力吐槽 (
这样写代码会被看你代码的人骂的,所以还是不要这样写…… ^_^
这样写代码会被打死系列
[…] by lovekay·0评论 var_dump(0…9)输出什么? php […]
第一个代码块中的引号是中文的🧐
wordpress特别2 , 自动转
新的PHP面试题(坑)诞生了
这种当面试题,你还是不要坑别人了。不是神仙都做不出来