Why is the PHP string concatenation operator a dot (.)?

27,402

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
27,402
Justin Ethier
Author by

Justin Ethier

Software Engineer living in the Baltimore area. Open Source Projects - A brand-new compiler that allows practical application development using R7RS Scheme. - A practical implementation of the Scheme programming language for the Haskell Platform. stack-watch - A unix command-line utility to automatically monitor Q&A activity on Stack Exchange. node-kdtree - A node.js add-on for performing efficient Nearest Neighbor searches using libkdtree. Minor contributions to many projects including jsgauge, jqGrid, Highcharts, Haskell SELinux bindings, chibi-scheme, jQuery UI Spinner, jClock, and the jQuery Validation Plugin. Featured solutions to Ruby Quiz Mexican Blanket Vehicle Counters If you develop a program for a long period of time by only adding features but never reorganizing it to reflect your understanding of those features, then eventually that program simply does not contain any understanding and all efforts to work on it take longer and longer. There is no right answer ... and always a better way. Show and discuss your code, without emotional attachment. You are not your code. Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. The game isn't really about big edges and firework displays; it's about subtle advantages and what happens in the long run. It is amazing what you can accomplish if you do not care who gets credit. Maybe the best programmers aren’t those who spectacularly solve crazy problems, but those who don’t create them, which is much more silent.

Updated on May 17, 2021

Comments

  • Justin Ethier
    Justin Ethier almost 3 years

    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"
    
  • Marco Demaio
    Marco Demaio over 11 years
    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
    Marco Demaio over 11 years
    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!
  • traq
    traq over 11 years
    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?).
  • nnnnnn
    nnnnnn almost 11 years
    ""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
    nnnnnn almost 11 years
    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.)
  • Alan Plum
    Alan Plum over 9 years
    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.