为什么"..."在我的代码中连接两个数字?

编程入门 行业动态 更新时间:2024-10-16 20:28:53
本文介绍了为什么"..."在我的代码中连接两个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码片段,在这些代码片段中我不太理解其输出:

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"

更多推荐

为什么"..."在我的代码中连接两个数字?

本文发布于:2023-10-27 10:55:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   代码   数字   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!