为什么用句号“."而不是用于PHP中字符串连接的加号"+"? [英] Why a full stop, "." and not a plus symbol, "+", for string concatenation in PHP?

查看:204
本文介绍了为什么用句号“."而不是用于PHP中字符串连接的加号"+"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP的设计者为什么决定使用句号/句点/.作为字符串 并置运算符,而不是更常见的加号"+"?

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 "+" ?

它是否有优势,还是有任何理由?还是他们只是喜欢? :o)

Is there any advantage to it, or any reason at all? Or did they just like to? :o)

推荐答案

最明显的原因可能是PHP从Perl继承了很多语法-Perl对字符串使用点(.)串联.

The most obvious reason would probably be that PHP inherits a lot of its syntax from Perl - and Perl uses a dot (.) for string concatenation.

但是,我们可以更深入地研究它,并弄清楚为什么它在Perl中实现-+运算符最常用于数学方程式-仅用于以变量类型可以定义方式的语言的级联运算符在其中工作(简单的解释,例如C#)

But, we can delve deeper into it and figure out why this was implemented in Perl - the + operator is most commonly used for mathematical equations - it's only used for concatenation in languages in which the variable type can define the way in which the operator works (simple explanation, example is C#)

var intAddition = 1 + 2;
Console.WriteLine(intAddition); // Prints 3
var stringConcat = "1" + "2";
Console.WriteLine(stringConcat); // Prints "12"

^如您所见,+运算符用于C#的串联和加法.

^ As you can see, the + operator is used both for concatenation and addition in C#.

也许推理的层次较低,并且是由于逻辑门的布尔代数-+在逻辑门中表示OR,而.用作AND运算符-在涉及字符串串联时很有意义.

Perhaps the reasoning goes lower level and is due to the boolean algebra of logic gates - + means OR in logic gates, whereas . is used as the AND operator - which makes sense when it comes to string concatenation.

有两个单独的运算符是有意义的,一个用于连接,另一个用于加法-不幸的是,由于其他语言,这两个运算符可以混合在一起.

It makes sense to have two separate operators, one for concatenation and one for addition - it's just unfortunate that these two can be mixed up due to other languages.

这篇关于为什么用句号“."而不是用于PHP中字符串连接的加号"+"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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