为什么 PHP 字符串连接运算符是点 (.)?
在 PHP 中,字符串运算符 点 (.) 用于连接字符串。例如:
$msg = "Hello there, " . $yourName;
点运算符似乎总是让人们(包括我自己)第一次看到它时感到困惑,特别是当你使用它来连接两个字符串时,该操作不会抛出错误,而只是“默默地”失败。在 PHP 和其他不使用此运算符的语言(例如 JavaScript、Python 等)之间切换时,这也是一个常见错误。
为什么该语言使用点 (.) 运算符而不是更广泛接受的运算符,例如加号 (+)?您可以指出选择该运营商的历史原因吗?仅仅是因为点可以将其他变量类型转换为字符串吗?例如:
echo 1 . 2; // Prints the string "12"
In PHP, the string operator dot (.) is used to concatenate strings. For example:
$msg = "Hello there, " . $yourName;
The dot operator always seems to confuse people (myself included) the first time they see it, especially since when you use it to concatenate two strings, the operation does not throw an error, but just "silently" fails. It is also a common mistake when switching between PHP and other languages such as JavaScript, Python, etc. that do not use this operator.
Why does the language use the dot (.) operator instead of a more widely accepted operator, such as plus (+)? Are there any historical reasons you can point to as to why this operator was selected? Is it just because the dot can cast other variable types to string? For example:
echo 1 . 2; // Prints the string "12"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为使用不同的运算符是个好主意,因为点和加做完全不同的事情。
从非特定语言的角度来看,
"a string" + "another string";
实际上意味着什么?这是否意味着
您会假设它是第二个,但在除字符串之外的所有情况下,加号都用于数字相加。为什么?
另外,从松散类型的角度来看(PHP 就是这样),php 脚本中的
点表示法因此指定它显然用于连接。
这并不是说它令人困惑,而是它不直观,因为所有其他语言都以不同的方式做到这一点。这是跟随趋势的好理由吗?按照一贯的方式做事并不总是正确的方式。
I think it is a good idea to have a different operator, because dot and plus do completely different things.
What does
"a string" + "another string";
actually mean, from a non specific language point of view?Does it mean
You would assume it is the second, but a plus sign is used for numerical addition in all cases except Strings. Why?
Also, from a loosely typed point of view (which PHP is), a php script
The dot notation therefore specifies that it is clearly used for concatenation.
It is not that it is confusing, it is that it is not intuitive because all the other languages do it in a different way. And, is this a good reason to follow the trend? Doing things the way they are always done, is not always the right way.
加号并不像您想象的连接字符串那样“被广泛接受”。有很多语言不使用它,包括 Perl 和 C,并且由于这些语言是 PHP 的根源所在,因此 PHP 效仿是有道理的。许多语言甚至没有对应的运算符;您必须使用
concat()
函数。PHP 是弱类型的,当它看到加号或点时会进行隐式类型转换。这意味着如果您执行
$x = "45inch" + "20inch";
,PHP 会将$x
设置为65
。如果使用点连接运算符,结果显然会非常不同。如果您有$y = 5 ,同样适用。 10;
。这将为您提供510
,但将其更改为加号,您会得到完全不同的结果。另外,从逻辑上思考,加号的相反是减号。但这并不那么容易映射到串联。 (我见过一种语言尝试过它,但它确实没有多大意义)
您对加号作为连接符的偏好纯粹是由于学习新语言时对改变的抵制(这是很常见的事情 - 我知道有些人最初讨厌 Python,因为它缺少大括号!)
作为一个长期使用多种语言进行编程的人,我可以告诉你,我更喜欢有一个明确的串联运算符。在松散类型语言中使用相同的运算符进行加法和串联是自找麻烦;事实上,我想说这是 Javascript 最大的缺陷之一(这是来自 Javascript 粉丝的人)。
Python 是强类型的,这意味着它可以不用使用加号作为加法和连接运算符,因为它迫使你使用相同的类型;在 Python 中,你不能将整数添加到字符串中;如果需要,则必须显式转换类型,这样就不存在歧义,至少对编译器而言不存在歧义。
然而,对于读者来说仍然存在歧义 - 通过阅读代码中任何给定加号的含义可能不会立即显而易见。在 Python 中更容易解决这个问题,但就我个人而言,我仍然更喜欢有一个明确的运算符。但这只是个人喜好;如果我使用 Python、Javascript 或 Visual Basic,那么我必须遵守它们的规则。
The plus sign is not as "widely accepted" as you would imagine for concatenating strings. There are a lot of languages which don't use it, including Perl and C, and since these are which are where PHP's roots lie, it makes sense for PHP to follow suit. Many languages don't even have an operator for it; you'd have to use a
concat()
function.PHP is weakly typed, and will do implicit type conversion when it sees the plus sign or a dot. This means that if you do
$x = "45 inches" + "20 inches";
, PHP will set$x
to65
. If you use the dot concatenation operator, the result will clearly be very different. The same applies if you have$y = 5 . 10;
. This will give you510
, but change it to a plus sign and you get a completely different result.Also, thinking logically, the opposite of a plus is a minus. But that doesn't map so easily to concatenation. (I have seen one language that tried it, but it really didn't make much sense)
Your preference for the plus sign as a concatenator is purely down to a resistance to change when learning a new language (quite a common thing - I know a few people who initially hated Python because it lacks curly braces!)
As someone who's programmed for a long time using a lot of languages, I can tell you that I much prefer to have an unambiguous concatenation operator. Using the same operator for addition and concatenation in a loosely-typed language is asking for trouble; in fact, I would say it's one of Javascript's biggest flaws (and this is coming from someone who in general is a fan of Javascript).
Python is stronly-typed, which means that it can get away with using the plus sign as the addition and concatenation operator because it forces you to work with the same type; you can't add an integer to a string in Python; if you need to then you have to explicitly cast your types, so there's no ambiguity, at least not to the compiler.
There is, however, still the ambiguity for the reader - it may not immediately be obvious from reading what was meant by any given plus sign in the code. It's easier in Python to work it out, but personally I'd still prefer to have an unambiguous operator. But that is just a personal preference; if I'm working with Python, Javascript or Visual Basic then I have to work to their rules.
在 PHP 中不可能使用 + 作为连接运算符,因为数字字符串和数字之间是等效的。您将有两个运算符使用相同的符号,并且
"3" + 3
的结果必须是未定义的。现在,"3" + 3
是6
,而"3" 。 3
是“33”
。It's not possible to use + as a concatenation operator in PHP, because of the equivalence between strings of digits and numbers. You'd have two operators using the same symbol, and the result from
"3" + 3
would have to be undefined. Now,"3" + 3
is6
, and"3" . 3
is"33"
.