What will var_dump(1...9) output? The following article will analyze it for you. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
A question, var_dump(1...9)
What is the output?
Verify it by hand:
php -r “var_dump(1...9)”; string(4) ”10.9“
Output 10.9. At first glance, the output of this var_dump seems strange, right? why?
Here we will teach you, if you see a piece of PHP code and feel that the output is strange, the first reaction is to see what the opcodes generated by this code are. Although this problem is actually a problem in the lexical analysis stage, but still use Let's analyze it with phpdbg (usually in order to prevent the influence of opcache, -n will be passed):
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
So it seems that long before the opcode is generated, 1...9 becomes the constant 10.9, considering This is a literal value. Let’s take a look at zend_language_scanner.l and find this line:
DNUM ({LNUM}?"."{LNUM})|({LNUM}"."{LNUM}?)
This is the format of floating point numbers defined by lexical analysis. It suddenly dawned on us at this point:
1 ...9 will be accepted in sequence as: 1. (floating point number 1), then . (string concatenation symbol) and then .9 (floating point number 0.9)
so it will be generated directly during the compilation phase "1" . "0.9" -> String literal "10.9"
Okay, here, this little "puzzle" is explained clearly.
Of course, this is not only defined in PHP, almost all languages will define this abbreviated floating-point form. Sometimes in C language, in order to input a floating-point integer, we can Use for example 1. to tell the compiler that this is a floating point number.
However, firstly, it happens to be in PHP. The number has another meaning, which is string concatenation. Secondly... in PHP5.6 Then there is a new operator called the Splat operator, which can be used to define variable parameter functions or solve arrays, for example,
<?php function foo($a, $b, $c) { var_dump($a + $b + $c); } $parameters = array (1, 2, 3); foo(...$parameters); ?>
So, at first glance, this leads to this confusing result
Recommended learning: PHP video tutorial
The above is the detailed content of What will php's var_dump(1...9) output?. For more information, please follow other related articles on the PHP Chinese website!