php php字符串数字连接搞砸了
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16294821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php string number concatenation messed up
提问by JustinHo
I got some php code here:
我在这里得到了一些 php 代码:
<?php
echo 'hello ' . 1 + 2 . '34';
?>
which outputs 234,
输出 234,
but when I add a number 11 before "hello":
但是当我在“你好”之前添加一个数字 11 时:
<?php
echo '11hello ' . 1 + 2 . '34';
?>
It outputs 1334 rather than 245(which I expected it to), why is that?
它输出 1334 而不是 245(我期望它),这是为什么?
回答by BlitZ
That's strange...
真奇怪...
But
但
<?php
echo '11hello ' . (1 + 2) . '34';
?>
OR
或者
<?php
echo '11hello ', 1 + 2, '34';
?>
fixing issue.
修复问题。
UPDv1:
UPDv1:
Finally managed to get proper answer:
终于设法得到正确的答案:
'hello'
= 0
(contains no leading digits, so PHP assumes it is zero).
'hello'
= 0
(不包含前导数字,因此 PHP 假定它为零)。
So 'hello' . 1 + 2
simplifies to 'hello1' + 2
is 2
, because no leading digits in 'hello1'
is zero too.
所以'hello' . 1 + 2
简化为'hello1' + 2
is 2
,因为没有前导数字'hello1'
也是零。
'11hello '
= 11
(contains leading digits, so PHP assumes it is eleven).
'11hello '
= 11
(包含前导数字,因此 PHP 假定它是 11)。
So '11hello ' . 1 + 2
simplifies to '11hello 1' + 2
as 11 + 2
is 13
.
因此'11hello ' . 1 + 2
简化了'11hello 1' + 2
为11 + 2
是13
。
UPDv2:
UPDv2:
http://www.php.net/manual/en/language.types.string.php
http://www.php.net/manual/en/language.types.string.php
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.
该值由字符串的初始部分给出。如果字符串以有效的数字数据开头,这将是使用的值。否则,该值将为 0(零)。有效的数字数据是一个可选的符号,后跟一个或多个数字(可选地包含小数点),后跟一个可选的指数。指数是“e”或“E”后跟一位或多位数字。
回答by naththedeveloper
The dot operator has the same precedenceas + and -, which can yield unexpected results.
点运算符与 + 和 -具有相同的优先级,这可能会产生意想不到的结果。
That technically answers your question... if you want numbers to be treated as numbers during concatination just wrap them in parenthesis.
从技术上讲,这回答了您的问题……如果您希望在连接期间将数字视为数字,只需将它们括在括号中即可。
<?php
echo '11hello ' . (1 + 2) . '34';
?>
回答by Yogesh Suthar
you have to use ()
in mathematical operation
你必须()
在数学运算中使用
echo 'hello ' . (1 + 2) . '34'; // output hello334
echo '11hello ' . (1 + 2) . '34'; // output 11hello334
回答by kav
You should check PHP type conversion table to get better idea what's happen behind the scenes: http://php.net/manual/en/types.comparisons.php
您应该检查 PHP 类型转换表以更好地了解幕后发生的事情:http: //php.net/manual/en/types.comparisons.php
回答by chandresh_cool
If you hate putting operators in between assign them to vaiable
如果您讨厌将运算符放在中间,请将它们分配给 vaiable
$var = 1 + 2;
echo 'hello ' . $var . '34';