This question already has an answer here:
这个问题已经有了答案:
- Why is '…' concatenating two numbers in my code? 1 answer
- 为什么在我的代码中连接两个数字?1回答
I've just faced a little PHP snippet from 3v4l: https://3v4l.org/jmrZB
我刚刚看到了3v4l中的一小段PHP代码: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?
恐怕我不知道如何解释它的结果。为什么这被认为是有效的呢?
4 个解决方案
#1
219
The dot (.
) has two roles in PHP:
dot(.)在PHP中有两个角色:
- 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 both1.
and.1
are valid real numbers in PHP but.
is not a number. - 当它是实数的一部分时,例如,1。1。积分部分和小数部分在实数上都是可选的,但不能同时进行。这意味着两个1。1在PHP中是有效的实数。不是一个数字。
- 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',它的值是字符串'11'。
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.
表达式1…1被解析为1。根据上面所说的,1。1是实数(1.0和0.1),中间点(.)是字符串连接操作符。
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.
当将数字转换为字符串时,PHP使用该操作所需的最小字符数。如果实数只有整数部分,那么它表示整数,没有小数点和小数。
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是一样的。“0.1”,表达式的最终值是10.1。
Why is 1...1
parsed this way?
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表示一个数从这里开始。1。是一个有效的实数,但是。不是。它使1。作为一个数字,下一个点是连接操作符。下一个数字后面跟着一个数字,是另一个实数(.1)的开始。
All in all, 1...1
is the same as 1. . .1
.
总之,1…1等于1。
#2
61
Because it's interpreted as 1.
.
.1
(1
and 0.1
) therefore you get 10.1
因为它被解释为1。1(1和0。1)所以你得到10。1
#3
45
Applying braces will make it clear:
使用牙套会让你明白:
(1.) . (.1)
(1)。(1)
-
1.
is interpreted as1
- 1。被解释为1
-
.
is string concatenation - 。是字符串连接
-
.1
is interpreted as0.1
- .1被解释为0.1
This all put into a string is 10.1
as a string.
这些都放到一个字符串中是10.1个字符串。
var_dump(1...1)
yieldsstring(4) "10.1"
var_dump(1,1)yieldsstring(4)“10.1”
#4
22
Different Operations.
不同的操作。
1. <?php echo 1.1; ?> // gives simple 1.1
2. <?php echo 1...1; ?> // gives 10.1
3. <?php echo 1..'1'; ?> // gives 11
4. <?php var_dump(1.); ?> // gives 1
5. <?php var_dump(.1); ?> // gives 0.1
Now, our strange operation
现在,我们奇怪的操作
echo 1...1
is treated as concatenation of no 4 and no 5, resulting in 10.1
被视为4和5的连接,导致10.1
#1
219
The dot (.
) has two roles in PHP:
dot(.)在PHP中有两个角色:
- 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 both1.
and.1
are valid real numbers in PHP but.
is not a number. - 当它是实数的一部分时,例如,1。1。积分部分和小数部分在实数上都是可选的,但不能同时进行。这意味着两个1。1在PHP中是有效的实数。不是一个数字。
- 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',它的值是字符串'11'。
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.
表达式1…1被解析为1。根据上面所说的,1。1是实数(1.0和0.1),中间点(.)是字符串连接操作符。
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.
当将数字转换为字符串时,PHP使用该操作所需的最小字符数。如果实数只有整数部分,那么它表示整数,没有小数点和小数。
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是一样的。“0.1”,表达式的最终值是10.1。
Why is 1...1
parsed this way?
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表示一个数从这里开始。1。是一个有效的实数,但是。不是。它使1。作为一个数字,下一个点是连接操作符。下一个数字后面跟着一个数字,是另一个实数(.1)的开始。
All in all, 1...1
is the same as 1. . .1
.
总之,1…1等于1。
#2
61
Because it's interpreted as 1.
.
.1
(1
and 0.1
) therefore you get 10.1
因为它被解释为1。1(1和0。1)所以你得到10。1
#3
45
Applying braces will make it clear:
使用牙套会让你明白:
(1.) . (.1)
(1)。(1)
-
1.
is interpreted as1
- 1。被解释为1
-
.
is string concatenation - 。是字符串连接
-
.1
is interpreted as0.1
- .1被解释为0.1
This all put into a string is 10.1
as a string.
这些都放到一个字符串中是10.1个字符串。
var_dump(1...1)
yieldsstring(4) "10.1"
var_dump(1,1)yieldsstring(4)“10.1”
#4
22
Different Operations.
不同的操作。
1. <?php echo 1.1; ?> // gives simple 1.1
2. <?php echo 1...1; ?> // gives 10.1
3. <?php echo 1..'1'; ?> // gives 11
4. <?php var_dump(1.); ?> // gives 1
5. <?php var_dump(.1); ?> // gives 0.1
Now, our strange operation
现在,我们奇怪的操作
echo 1...1
is treated as concatenation of no 4 and no 5, resulting in 10.1
被视为4和5的连接,导致10.1