为什么 php 字符串连接运算符是一个点 (.)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4266799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the php string concatenation operator a dot (.)?
提问by Justin Ethier
In PHP, the string operatordot (.) is used to concatenate strings. For example:
在 PHP 中,字符串运算符点 (.) 用于连接字符串。例如:
$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 2 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.
点运算符似乎总是让人们(包括我自己)第一次看到它时感到困惑,特别是因为当您使用它连接 2 个字符串时,该操作不会抛出错误,而只是“无声地”失败。在 PHP 和其他不使用此运算符的语言(例如 JavaScript、Python 等)之间切换时,这也是一个常见的错误。
My question is, 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"
Thanks!
谢谢!
采纳答案by Codemwnci
I think it is a good idea to have a different operator, because dot and plus do completely different things.
我认为使用不同的运算符是个好主意,因为 dot 和 plus 做完全不同的事情。
What does "a string" + "another string";
actually mean, from a non specific language point of view?
"a string" + "another string";
从非特定语言的角度来看,实际上是什么意思?
Does it mean
是不是意味着
- Add the numerical value of the two strings, or,
- concatenate the two strings
- 将两个字符串的数值相加,或者,
- 连接两个字符串
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
此外,从松散类型的角度(PHP 是)来看,一个 php 脚本
$myvar = 1;
$myvar2 = 2;
// would we expect a concatenation or addition?
$concat = $myvar + $myvar2;
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.
并不是它令人困惑,而是它不直观,因为所有其他语言都以不同的方式做到这一点。而且,这是跟风的好理由吗?按照他们一贯的方式做事,并不总是正确的方式。
回答by Spudley
The plus sign is not as "widely accepted" as you would imagine for concatenating strings. There are a lotof 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.
对于连接字符串,加号并不像您想象的那样“被广泛接受”。有很多语言不使用它,包括 Perl 和 C,并且因为这些是 PHP 的根源所在,所以 PHP 效仿是有意义的。许多语言甚至没有操作符。你必须使用一个concat()
函数。
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
to 65
. 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 you 510
, but change it to a plus sign and you get a completely different result.
PHP 是弱类型的,当它看到加号或点时会进行隐式类型转换。这意味着如果您这样做$x = "45 inches" + "20 inches";
,PHP 将设置$x
为65
. 如果使用点连接运算符,结果显然会大不相同。如果您有$y = 5 . 10;
. 这会给你510
,但把它改成加号,你会得到一个完全不同的结果。
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!)
你喜欢用加号作为连接符纯粹是因为在学习一门新语言时拒绝改变(很常见 - 我知道一些人最初讨厌 Python,因为它没有花括号!)
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).
作为一个长期使用多种语言进行编程的人,我可以告诉你,我更喜欢有一个明确的连接运算符。在松散类型的语言中使用相同的运算符进行加法和连接是自找麻烦;事实上,我会说这是 Javascript 最大的缺陷之一(这来自一个通常是 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.
Python 是 stronly 类型的,这意味着它可以避免使用加号作为加法和连接运算符,因为它迫使您使用相同的类型;你不能在 Python 中向字符串添加整数;如果需要,则必须显式转换类型,因此没有歧义,至少对编译器没有歧义。
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.
然而,对于读者来说,仍然存在歧义——通过阅读代码中任何给定加号的含义,可能不会立即显而易见。在 Python 中更容易解决它,但我个人仍然更喜欢有一个明确的运算符。但这只是个人喜好;如果我使用 Python、Javascript 或 Visual Basic,那么我必须遵守他们的规则。
回答by Rosh Oxymoron
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
is 6
, and "3" . 3
is "33"
.
在 PHP 中不能使用 + 作为连接运算符,因为数字和数字字符串之间是等价的。您将有两个使用相同符号的运算符,并且结果 from"3" + 3
必须是未定义的。现在,"3" + 3
是6
,"3" . 3
是"33"
。