PHP的字符串数串联混乱 [英] php string number concatenation messed up

查看:52
本文介绍了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' + 22,因为'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 + 213.

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.

这篇关于PHP的字符串数串联混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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