42

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"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
  • 20
    Using a + wouldn't be particularly practical in a loosely-typed language: should echo 1 + 2; give 12 or 3? – Mark Baker Nov 24 '10 at 12:37
  • "Is it just because the dot can cast other variable types to string?" As everything, which expects a string cast primitive type to strings: Yes, thats the reason ;) – KingCrunch Nov 24 '10 at 12:48
  • I would love the `+` operator for string concatenation in PHP, the `.` operator is only confusing and it's one of the few things I hate most about PHP. – Marco Demaio Dec 02 '12 at 17:43
  • 1
    How is this not market as non-constructive? – umassthrower Mar 25 '13 at 23:34
  • Programming questions without concrete answers fit better on http://programmers.stackexchange.com/ – Cees Timmerman Nov 20 '13 at 17:28
  • 3
    16,000 people have seen this thread as of 2017--how is this not constructive? – duhaime Dec 02 '17 at 19:41
  • 2
    Missing from the answers here is the actual reason why PHP uses "." for concatenation: Because it borrowed it from Perl. PHP was originally a set of Perl scripts, and borrowed a number of things from the Perl syntax. Why Perl uses "." for concatenation, I don't know :) – Alice Heaton Dec 02 '20 at 11:38
  • Duplicate (asked one year earlier, with 15 answers): *[Why a full stop, “.” and not a plus symbol, “+”, for string concatenation in PHP?](https://stackoverflow.com/questions/1866098)* – Peter Mortensen May 16 '21 at 10:43

3 Answers3

32

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

  • 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

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

Codemwnci
  • 54,176
  • 10
  • 96
  • 129
  • 8
    I do appreciate your logical reasons, but I totally disagree. The dot is just confusing and one of the few things that I really hate about PHP. – Marco Demaio Dec 02 '12 at 17:36
  • 11
    *Why* is it "just confusing"? Your comment above would seem to imply nothing more than a personal preference for the `+` operator (which I would suspect comes from your experience with other languages that use it?). – traq Jan 03 '13 at 06:56
  • 7
    I think it's at least a _little_ bit confusing that `1 . 2` is not equal to `1.2`. And somebody with a maths background might think `$x . $y` meant `$x` multiplied by `$y`. (But obviously anybody starting to code in a new language should learn the basic operators for that language.) – nnnnnn May 20 '13 at 13:16
  • 3
    The argument is valid, but the premise is wrong. PHP creates the ambiguity that the dot operators solves because of its weak type system. It's a trade-off between stricter variable types and stricter operation argument types: in JavaScript "+" is generally unambiguous because you're expected to convert user input into the right type before using it; in PHP "+" needs to enforce its argument types (i.e. convert its arguments into numbers automagically) because you're expected to be able to just pass around user input as strings. This is part of the reason why so many consider PHP to be sloppy. – Alan Plum Sep 13 '14 at 13:12
10

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

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 8
    `C` language does not use `+` operator because it does not have a string concatenation operator at all! You can overload class operators only in `C++` and I still remember that one of the 1st thing I did while learing `C++` was a `MyString` class with the `+` operator used to concatenate strings. It was cool! – Marco Demaio Dec 02 '12 at 17:40
6

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

Rosh Oxymoron
  • 20,355
  • 6
  • 41
  • 43
  • 4
    _""3" + 3 would have to be undefined."_ - It wouldn't _have_ to be undefined, it could just cast to a string like many other languages manage to do without unraveling the very fabric of time and space. – nnnnnn May 20 '13 at 13:14