我有以下代码片段,在这些代码片段中我不太理解其输出:
I have the following code snippet where I don't really understand its output:
echo 20...7;为什么此代码输出200.7?
据我所知 ... 是splat运算符,在ruby中称为splat运算符,它使您可以使用具有可变数量参数的函数,但是在使用echo的上下文中,我不明白它在做什么
From what I know ... is the splat operator, which it is called in ruby, that lets you have a function with a variable number of arguments, but I don't understand what it does here in the context with echo.
任何人都可以解释这段代码的确切作用吗?
Can anyone explain what exactly this code does?
推荐答案不,这不是splat/unpacking运算符,甚至认为它看起来也不是.这只是PHP解析过程的结果.已经编写了一些不同的代码可能会消除一些困惑:
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现在,您必须知道.7是0.7,并且可以按照浮点数语法:
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]*)因此,PHP仅将这两个数字连接在一起,而在进行此操作时,PHP的类型杂耍将将两个数字都静默转换为字符串.
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"更多推荐
为什么"..."在我的代码中连接两个数字?
发布评论