-1

During a conference that I attended some time ago, a .NET programmer that was a speaker there asked a question:

"Why is the concatenation operator in PHP different from the other languages?"

More exactly, why is it a dot instead of a plus?

At that time, nobody present knew the answer. Today, while playing with JavaScript I think that I found one reason. I'm curios if this is indeed the main reason why it's a dot instead of a plus.

I'll use JavaScript for the example:

var foo = 7;
var bar = 3;
console.log(foo+bar);

In this case, both variable's type is integer so the plus will behave the same way as it does in math, calculating the sum. The result will be 10.

Next scenario:

var foo = 7;
var bar = "3";
console.log(foo+bar);

In this case "foo" is an integer and "bar" is a string. The result would be 73.

I guess that a dot was used instead of a plus to kill some ambiguity regarding this particular case.

Just before posting this question I noticed a similar question on SO that have a similar answer to what I discovered today.

This is the link to the question: Why is the php string concatenation operator a dot (.)?

Are there any more reasons besides this? Thank you for your time and please let me know if I need to edit my question before downrating. :)

Community
  • 1
  • 1
SporeDev
  • 608
  • 1
  • 8
  • 26
  • 3
    People need to stop asking "Why?" of PHP. PHP does PHP things PHP's way for PHP's sake. – Ignacio Vazquez-Abrams Dec 04 '13 at 20:09
  • philosophy overflow is that way >>>> –  Dec 04 '13 at 20:10
  • 4
    actually, using same operator for concatenation and addition is usually considered a **design mistake** in programming languages. – tereško Dec 04 '13 at 20:12
  • It's a design mistake in languages that make the design mistake of not being able to tell between numbers and text. – Ignacio Vazquez-Abrams Dec 04 '13 at 20:13
  • It is not a language design mistake. – Rok Kralj Dec 04 '13 at 20:15
  • `(1 + 3) + 'a'` and `1 + (3 + 'a')` ... should these be equal or not, and **why**? And then how about, if you hve `1 + 'a' + 3` and `1 + 3 + 'a'`? – tereško Dec 04 '13 at 20:16
  • 1
    Both PHP and Javascript perfectly know how to tell between a number and a string, they just choose to let the users be lazy and try to make sensible assumptions. This is quite practical but requires a bit of discipline, true... The fact that PHP allows you to increment an array bothers me much more :) – djfm Dec 04 '13 at 20:16
  • I love these questions. These and the ones where people hold shift and smash their keyboard then ask why the output is what it is. ` – Mike B Dec 04 '13 at 20:17
  • @tereško, yes, those wild type castings break associativity - this is shocking for my math background – djfm Dec 04 '13 at 20:18
  • 1
    When thinking about why PHP is designed the way it is, I generally assume that alcohol played a major role. – andrewsi Dec 04 '13 at 20:21

1 Answers1

5

PHP was originally developed in Perl, from which it borrowed a lot of its syntax. This includes the use of . as a string concatenation operator, and the prefix $ for variable names.

r3mainer
  • 23,981
  • 3
  • 51
  • 88