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

15,970

Solution 1

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.

Solution 2

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?

Solution 3

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

Solution 4

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...

Solution 5

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

Share:
15,970
Caroline
Author by

Caroline

Updated on June 18, 2022

Comments

  • Caroline
    Caroline almost 2 years

    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 advantage to it, or any reason at all? Or did they just like to? :o)

  • RichardOD
    RichardOD over 14 years
    Confusing if you are not used to it. In Java and C# this is the norm.
  • mauris
    mauris over 14 years
    yeah i know. in Java i had to do this instead: "this is a string " + (56 + 20)
  • joshcomley
    joshcomley over 14 years
    Doesn't really answer the question "why the use of a period". It just defers the answer.
  • Daniel May
    Daniel May over 14 years
    For clarity - $stringresult returns "10020" and $arithmeticresult returns 120.
  • Ben James
    Ben James over 14 years
    joshcomley: that was a valid point, so I have elaborated. Initially I took the reference to "the designers of PHP" at face value
  • mpeters
    mpeters over 14 years
    Actaully Perl 6 now uses the "~" for string concatentaion.
  • Ben James
    Ben James over 14 years
    mpeters: Thanks, it seems I was behind on this news, I have edited accordingly.
  • Ian
    Ian over 14 years
    Not sure where you've got Perl Home Page from, but in 12 yrs of PHP development, I've never heard it called this: "Personal Home Page Tools" maybe
  • Ian
    Ian over 14 years
    Oh and it currently stands for "PHP Hypertext Preprocessor"
  • Skilldrick
    Skilldrick over 14 years
    I agree with Doug. Once you get used to the syntax it's much clearer and less ambiguous - both worthwhile aims for syntax.
  • Pavel Minaev
    Pavel Minaev over 14 years
    You use some sane operator, like & which VB uses, or ~ which D (and, apparently, Perl6) uses, or .. which Lua uses (which is IMO still weird, but still more understandable).
  • Pavel Minaev
    Pavel Minaev over 14 years
    How often do you use string concatenation to concatenate sentences? Also, dot is a sentence terminator, not a sentence separator, so analogy breaks anyway.
  • Pavel Minaev
    Pavel Minaev over 14 years
    Even in math, addition is not always commutative; e.g. not for ordinal numbers.
  • Ken
    Ken over 14 years
    Analogy is imperfect, film at 11.
  • jimtut
    jimtut over 14 years
    @Pavel: huh? I've never heard of anyone trying to add ordinals. How does First + Second even compute? Is the answer Third? If so, why wouldn't Second + First also equal Third?
  • Ken
    Ken over 14 years
    Being "used to it" has nothing to do with it. I've been writing C# for a couple years, and before that Java since around 1999, and this ambiguous crap still confuses me. (In comparison, Haskell's string concatenation operator took me about 10 seconds to figure out.) Examples of crappy syntax are like assholes: every language has one and they all stink.
  • Sean McMillan
    Sean McMillan over 14 years
    Yeah; Javascript + wrecks my brain on a regular basis, usually because it starts concatenating when I didn't want it to.
  • Sean McMillan
    Sean McMillan over 14 years
    Grr at "untyped variables". That's an oversimplification.
  • Alex Stoddard
    Alex Stoddard over 14 years
    The de facto standardization on "." as the method operator post dates the adoption of "." for concatenation in Perl which then influenced PHP. And perl initially had no object orientation. It is precisely because everyone now expects "." to indicate methods on objects that Perl 6 has adopted new syntax (from a perl point of view) allowing "." to be used for methods and using "~" for concatenation.
  • Paul Hsieh
    Paul Hsieh over 14 years
    @Pavel: Well if you interpret the original poster's question as one of aesthetics then you may have a point -- I would prefer '&' myself as well, but I thought the technical explanation was what was really being asked for in this case (they thought using '+' for both cases was ok).
  • Robert P
    Robert P over 14 years
    It's not so much that they thought it was a bad choice, as it was that the . operator has become the defacto method accessor in almost all languages. Back then, it was a fine choice. Now they want to use it for a different purpose.
  • TheHippo
    TheHippo over 14 years
    Perl Home Page Tools is right. In the german version of the wikipedia article it is mentioned right in the first sentence: de.wikipedia.org/wiki/PHP
  • Sarfraz
    Sarfraz over 14 years
    Why do you put a dot in-between sentences? You should not include one i suppose :)
  • Jacob
    Jacob over 14 years
    I'm sure that I read that it was called Perl Home Page, sometime before it was released. That could just my mind playing tricks on me though. At any rate, I removed that item from the list, because this post seemed better without it.
  • DisgruntledGoat
    DisgruntledGoat over 14 years
    @Pavel: Why is . not a "sane operator" for concatenation? And just because you're used to using . for object members doesn't make it the most logical operator for that. (PHP's choice of -> is quite good IMO.)
  • Corey Ballou
    Corey Ballou over 14 years
    Because if you used + for concatenation, like in Javascript, you run into typing issues due to loosely typed languages. The lexical parser has a hard time determining whether you want to do addition or concatenation.
  • Andrew Noyes
    Andrew Noyes over 14 years
    -> isn't attrocious, but the most common use of that operator is in C++ as a shorthand to dereference pointers to objects or structs. Say there's a struct with a member x and you have a pointer p to a struct of that type. Rather than using (*p).x = 5;, you would use p->x = 5;. Given PHP's C-like lineage, I think this overloads the syntax, even if there's no use for that operator in this context in PHP.
  • Ben James
    Ben James about 14 years
    + for concatenation is one of the few things that regularly catches me out in JS
  • visual_learner
    visual_learner about 14 years
    @Andrew - No. PHP inherits teh -> syntax from Perl. In Perl, to dereference a hash reference, you use my $hash = { this => "that" }; $hash->{this}. It is conceptually performing a "dereference and then access", which is the same thing it does in C.
  • sun
    sun over 10 years
    Hi, i am guessing one more reason but i don't know whether it is correct or not. The one more reason might be we are not specifying datatype for the variable right, without knowing string or int it can't concatenate, so they might have avoided this. of course var datatype is there but even i guess this. What you say? @Daniel May