Why is '…' concatenating two numbers in my code?

后端 未结 1 731
遇见更好的自我
遇见更好的自我 2021-01-04 06:47

I have the following code snippet where I don\'t really understand its output:

echo 20...7;

Why does this code output 200.7?

1条回答
  •  醉梦人生
    2021-01-04 06:58

    No this is not the splat/unpacking operator, even thought it might seem like it is. This is just the result of the PHP parsing process. Already writing your code a bit different might clear some confusion:

    echo  20.           .           .7;
    #       ↑           ↑           ↑
    #    decimal  concatenation  decimal
    #      dot         dot         dot
    

    Now you have to know that .7 is 0.7 and you can omit the 0 in PHP as described in the syntax for float numbers:

    DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
    

    So PHP just concatenates those two numbers together and while doing this PHP's type juggling will silently convert both numbers to strings.

    So in the end your code is equivalent to:

    echo "20" . "0.7";
    //Output: "200.7"
    

    0 讨论(0)
提交回复
热议问题