|
The string concatenation operator in PHP is '.', not '+'. Using the '+' operator will convert the two strings to numeric types and then add the resulting values. If the strings are not numeric, then they'll cast to 0, as you've seen. And, as an interesting side-note, if a string starts with a number but contains non-numeric characters, PHP will truncate everything from the first non-numeric and convert just the leading digits to an int.
And no, it wouldn't be great if it just concatenated them, because then you'd have to worry about the contents of the string. Should the result of "123" + "456" be 579 or "123456"? What about "123" + "5.2x"? Is it really a good thing to have to worry about that? PHP has enough gotchas already without adding ambiguity to one of the standard operators.
|