Why a full stop, “.” and not a plus symbol, “+”, for string concatenation in PHP?

前端 未结 12 1609
暖寄归人
暖寄归人 2020-12-09 01:54

Why did the designers of PHP decide to use a full stop / period / \".\" as the string concatenation operator rather than the more usual plus symbol \"+\" ?

Is there

12条回答
  •  自闭症患者
    2020-12-09 02:22

    + should always be defined as a commutative operation (i.e., A+B = B+A). In the case of string concatenation, this is not the case ("foo" + "bar" != "bar" + "foo"). As such, + is not a good operator to use for the concatenation operation. Whether or not the language authors had this in mind when they used . instead (which is close to the multiplication operator, for which commutativity need not hold) remains to be seen, but it was a good decision nonetheless.

提交回复
热议问题