0

Possible Duplicate:
Why is the php string concatenation operator a dot (.)?

I've always wondered -

Why does PHP use the . operator to concat strings instead of the + sign?

Is this some sort of way to improve script-evaluation performance?

Thanks

Community
  • 1
  • 1
Alon
  • 801
  • 1
  • 10
  • 22

3 Answers3

2

Because otherwise, what would happen in this case?

$str = "4";
$num = 2;

$result = $str + $num;

What if you wanted the result to be "42"?

Clarification

The above answers the question "why is there an operator . in addition to the operator +?". If the intended question was "why does operator + not perform string concatenation?" (with the understanding that the would need to be another operator to take over the current behavior +), then I 'll be happy to remove my answer in favor of a more relevant one.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Will the gentleman who downvoted please provide an explanation? – Jon Mar 10 '11 at 16:25
  • This is not the reason PHP uses `.` for string concatenation. There are languages that use `+` for string concatenation and `"4"+2` yields `"42"`. – Gumbo Mar 10 '11 at 16:27
  • @Gumbo: Sure. But then you would need another operator to enable `"4" + 2 == 6`, so it's chicken-and-egg. – Jon Mar 10 '11 at 16:28
  • @Jon: That depends on the implicit type conversion policy. In JavaScript, for example, the implicit type conversion is not to convert to string only if both operands are not numbers: `4 + 2 === 6`, `"4" + 2 === "42"`, `4 + "2" === "42"`, and `Number("4") + 2 === 6`. – Gumbo Mar 10 '11 at 16:41
  • @Gumbo: That doesn't address the issue. You would still need some operator `@` so that `"4" @ "2" == 6`. – Jon Mar 10 '11 at 16:43
  • @Jon: No, why would you? PHP has a weak typing system and it uses implicit type conversion before applying an operation with incompatible operand types. In languages with strong typing you would need an explicit type conversion. – Gumbo Mar 10 '11 at 16:50
  • @Gumbo: I didn't phrase that clearly. I mean that you 'd need an operator `@` in a world where `"4" + "2" == "42"`; clearly there has to be *some* way of indicating which operation you want to perform. – Jon Mar 10 '11 at 16:57
  • @Jon: The [operation](http://en.wikipedia.org/wiki/Operation_(mathematics)) depends on the type of its operands. – Gumbo Mar 10 '11 at 17:13
  • @Gumbo: Please don't make this needlessly hard :) Yes, technically you would not need an *operator* in the sense that you could provide an explicit type conversion or cast and get the job done. But the designers of a language which forced you to do what would probably die of shame. – Jon Mar 10 '11 at 17:23
1

After deciding that it (PHP) would do lots of autoboxing there was pretty much no other choice then to use 2 different operators for "adding" and "concating".

"+" for adding is obvious and @Gumbo explained why "." was chosen.

var_dump("12ab" + "34cd"); // 46
var_dump("12ab" . "34cd"); // "12ab34cd"

so you need to tell the language that you want it to do because it can do both :)


Other languages don't have that problem because they don't allow the implicit conversion from a string to an integer.

So if you write "4" + 2 the language would tell you that it can't to that and you'd need to write: intval("4") + 2 and it knows what to do.

Also see here

why-is-the-php-string-concatenation-operator-a-dot

Community
  • 1
  • 1
edorian
  • 38,542
  • 15
  • 125
  • 143
0

Because Perl used the . for string concatenation and PHP was highly influenced by Perl’s syntax.

Gumbo
  • 643,351
  • 109
  • 780
  • 844