PHP连接字符串数学运算 [英] php concatenating string math operation

查看:67
本文介绍了PHP连接字符串数学运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$a =  'hello' . 3 + 6 + 10;
echo $a; // 16

我希望它不是19。

我知道我可以将数学运算放在()中:

I know I can put the math operation in ():

$a =  'hello' . (3 + 6 + 10);
echo $a; // hello19

但是为什么php返回16?

But why is php returning 16?

先谢谢。

推荐答案

在PHP中都 + 等于优先级,并且都保持关联。

In PHP both . and + have equal precedence and are both left associative.

结果

'hello' . 3 + 6 + 10;

被评估为

('hello' . 3) + 6 + 10;

= 'hello3' + 6 + 10                           

= ('hello3' + 6) + 10 // String 'hello3' when interpreted as a number gives 0
                      // as it starts with a non-digit.

= 6 + 10

= 16

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

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