Why a full stop, "." and not a plus symbol, "+", for string concatenation in 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 "+" ?
Is there any adv开发者_StackOverflowantage to it, or any reason at all? Or did they just like to? :o)
PHP's syntax is influenced by Perl, and .
is the string concatenation operator in Perl.
In a weakly typed language there are advantages to having a different string concatenation and numeric addition operators: which one you use will influence which type the language coerces the variables to.
As it happens, Perl 6 will use a tilde ~
instead of a dot .
for string concatenation, because .
will be used for object member access. So it seems the designers of Perl now think it was a bad choice.
Perhaps, in Perl and PHP's early, non-Object-Oriented days, it seemed like as good a choice as any. Maybe the designers of both languages never envisaged them becoming strong OO languages.
As for whether PHP will one day ditch its ->
member access syntax for .
, who knows?
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.
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"
^ As you can see, the +
operator is used both for concatenation and addition in C#.
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.
I am not a PHP expert, but, how else do you do differentiate that last two lines?
$first = 100;
$second = 20;
$stringresult = $first . $second; // "10020"
$arithmeticresult = $first + $second; // 120
Logically + is used for numbers. While a dot is used to concatenate two sentences (strings) in a paragraph for example. Hence dot is used to concatenate strings. So it is pretty logical i believe. It is better that way...
Douglas Crockford thinks that + for Concatenation is a Bad Idea:
JavaScript has its share of design errors, such as the overloading of + to mean both addition and concatenation with type coercion
The use of the dot as string concatenation operator in PHP probably dates back to Perl. Remember that PHP once was nothing more than a bunch of Perl scripts.
Also it makes sense to have distinct string concatenation and addition operators, especially in weakly-typed languages. There are enough pitfalls in PHP already to shoot yourself in the foot, you don't need to add another one.
I would too prefer to use a full stop instead of a plus sign because I usually associate + with mathematical equations.
For Example "this is a string " + 56 + 20
This would be very confusing for both the compiler/interpreter and the developer.
However the disadvantage to using full stop for concatenation operator is that it is just a dot on the screen and sometimes you can't see whether is it in the string or outside the string.
This doesn't answer the question, just wanted to share something.
From PHP Manual: String Operators, someone posted this which I find rather interesting. Notice how the space plays a part in the output.
Excerpt:
If you attempt to add numbers with a concatenation operator, your result will be the result of those numbers as strings.
<?php
echo "thr"."ee"; //prints the string "three"
echo "twe" . "lve"; //prints the string "twelve"
echo 1 . 2; //prints the string "12"
echo 1.2; //prints the number 1.2
echo 1+2; //prints the number 3
?>
+
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.
Here is a bit of historical context.
- The PHP language started out as a set of Perl scripts.
- As such, PHP gets most of it's syntax from Perl.
Perl, and by extension PHP, has untyped variables.
"5" == 5 "5" + 5 == 10 "5" . 5 == 55
- To be able to tell the difference between addition and concatenation, they had to be two different operators.
- Perl copied the method access operator from C
->
. - This was before many of the more modern programming languages started to use
.
for method access. - Concatenation is one of the more common operations, and should use fewer characters. According to Huffman coding.
.
was one of the few characters available for this use. The only other one that would make sense to use is~
, which is probably why that is now the Perl 6 concatenation operator.
While this isn't the historical reason it maybe show you why it is good to have a separate operator for addition and concatenation in the case of PHP.
The reason is what we see in JavaScript. We use the +
character for concatenation and addition too. In PHP we have a separate operator for the two which is important for dynamically typed languages where implicit type conversion also present.
In PHP
echo 5 + 1; //6
echo 5 . 1; //51
echo "5" + 1; //6
echo "5" . 1; //51
This isn't surprising for anyone because we always explicitly stated whether we want to add or concatenate the two variable. Or in other words if we think about them as numbers or as strings.
In JavaScript
console.log( 5 + 1); //6
console.log("5" + 1); //51
console.log( 5 + "1"); //51
console.log("5" + "1"); //51
Without the knowledge of how implicit type conversions happen under the hood, in many cases (especially if they are complex) surprising results can happen, because we don't know what will be the outcome, because it isn't explicitly stated.
In Python
Python also uses the +
operator for concatenation and for addition too, but it doesn't let implicit type conversion to happen. You have to explicitly mark the type conversion.
print 5 + 1 //6
print "5" + 1 //TypeError: cannot concatenate 'str' and 'int' objects
We have to do type conversion to make it work: a + int(b)
All in all
If we think about it, we can see that almost the same thing happens in PHP what we see in Python but instead of explicitly marking the type conversion, we mark with the operator that how we see the variables in terms of types.
I guess it is so you can concatenate numbers with strings?
$i=100;
$str="hello";
$str2 = $str.$i
Since you don't declare variable types, with a + it could give a result of 100 instead of "hello100."
精彩评论