Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" 
like image 997
Justin Ethier Avatar asked Nov 24 '10 12:11

Justin Ethier


People also ask

What is the string concatenation operator in PHP?

The first is the concatenation operator ('. '), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (' . = '), which appends the argument on the right side to the argument on the left side.

What is the symbol of concatenation operator in PHP?

The PHP concatenation operator (.) is used to combine two string values to create one string. Concatenation assignment. Example: <?

Can you concatenate strings in PHP?

Prepend and Append Strings in PHPYou can use the concatenation operator . if you want to join strings and assign the result to a third variable or output it. This is useful for both appending and prepending strings depending on their position. You can use the concatenating assignment operator .

Which operator is used for string concatenation?

The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.


2 Answers

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.

like image 105
Codemwnci Avatar answered Oct 01 '22 02:10

Codemwnci


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.

like image 30
Spudley Avatar answered Oct 01 '22 02:10

Spudley