为什么1 ... 1评估为10.1? [英] Why does 1...1 evaluate to 10.1?

查看:88
本文介绍了为什么1 ... 1评估为10.1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚面对3v4l的一些PHP代码段: https://3v4l.org/jmrZB

I've just faced a little PHP snippet from 3v4l: https://3v4l.org/jmrZB

echo 1...1; //10.1

恐怕我不知道如何解释其结果.为什么这被认为是完全有效的?

And I'm afraid I have no idea how to explain its results. Why is this considered valid at all?

推荐答案

点(.)在PHP中有两个作用:

The dot (.) has two roles in PHP:

  1. 作为十进制数字,如果它是实数的一部分,例如1.1.整数但不可同时是整数部分和小数部分都是可选的.这意味着1..1在PHP中都是有效的实数,但.不是数字.
  2. 作为字符串连接运算符.该运算符将两个字符串子表达式连接成一个较大的表达式.较大表达式的值是子表达式的字符串值的串联.不是字符串的子表达式将在连接之前转换为字符串.
    例如. 1 . 1'1' . '1'相同,其值是字符串'11'.
  1. As decimal digit, when it is part of a real number, e.g. 1.1. Both the integral part and the decimal part are optional on real numbers but not on the same time. This means both 1. and .1 are valid real numbers in PHP but . is not a number.
  2. As the string concatenation operator. This operator connects two string sub-expressions into a larger expression. The value of the larger expression is the concatenation of the string values of the sub-expressions. The sub-expressions that are not strings are converted to strings before concatenation.
    E.g. 1 . 1 is the same as '1' . '1' and its value is the string '11'.

表达式1...1被解析为1. . .1.根据上述内容,1..1是实数(1.00.1),中间点(.)是字符串连接运算符.

The expression 1...1 is parsed as 1. . .1. According to those said above, 1. and .1 are real numbers (1.0 and 0.1) and the middle dot (.) is the string concatenation operator.

将数字转换为字符串时,PHP使用此操作所需的最少字符数.如果实数仅包含整数部分,则它将实数表示为整数,不带小数点和小数.

When converts numbers to strings, PHP uses the minimum amount of characters required for this operation. If a real number has only integral part then it represents the number as integer, without decimal point and decimals.

这就是为什么1. . .1'1' . '0.1'相同且表达式的最终值为10.1的原因.

This is why 1. . .1 is the same as '1' . '0.1' and the final value of the expression is 10.1.

解析器从左到右读取表达式. 1告诉它一个从那里开始的数字. 1.是有效的实数,但1..不是.它将1.保留为数字,然后下一个点是串联运算符.下一个.,后跟一个数字,是另一个实数(.1)的开头.

The parser reads the expression from left to right. 1 tells it a number starts there. 1. is a valid real number but 1.. is not. It keeps 1. as a number then the next dot is the concatenation operator. The next ., being followed by a digit, is the beginning of another real number (.1).

总而言之,1...11. . .1相同.

这篇关于为什么1 ... 1评估为10.1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆