PHP的字符串数串联混乱
我在这里有一些php代码:
I got some php code here:
<?php
echo 'hello ' . 1 + 2 . '34';
?>
输出234,
但是当我在"hello"之前添加数字11时:
but when I add a number 11 before "hello":
<?php
echo '11hello ' . 1 + 2 . '34';
?>
它输出1334而不是245(我预期是245),为什么呢?
It outputs 1334 rather than 245(which I expected it to), why is that?
真奇怪...
但是
<?php
echo '11hello ' . (1 + 2) . '34';
?>
OR
<?php
echo '11hello ', 1 + 2, '34';
?>
修复问题.
UPDv1:
UPDv1:
最终设法获得正确答案:
Finally managed to get proper answer:
'hello'
= 0
(不包含前导数字,因此PHP假定其为零).
'hello'
= 0
(contains no leading digits, so PHP assumes it is zero).
因此'hello' . 1 + 2
简化为'hello1' + 2
是2
,因为'hello1'
中也没有前导数字也为零.
So 'hello' . 1 + 2
simplifies to 'hello1' + 2
is 2
, because no leading digits in 'hello1'
is zero too.
'11hello '
= 11
(包含前导数字,因此PHP假定它为11).
'11hello '
= 11
(contains leading digits, so PHP assumes it is eleven).
所以'11hello ' . 1 + 2
简化为'11hello 1' + 2
,因为11 + 2
是13
.
UPDv2:
UPDv2:
http://www.php.net/manual/zh/language.types.string.php
该值由字符串的初始部分给出.如果字符串 从有效的数字数据开始,这将是使用的值. 否则,该值将为0(零).有效数字数据是 可选符号,后跟一个或多个数字(可选地包含一个 小数点),然后是可选的指数.指数是 "e"或"E"后跟一个或多个数字.
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.