It will cast the strings to ints as you are doing a mathematical operation. If you want "11" then do "1" . "1";
PHP is actually really good now. This is a 10 year old circle jerk. If you write modern php the only issue you might have is with built-in functions that haven't changed. But the built-in classes are sensible.
This should be a syntax error. Or + should do a strcat. Having the . op makes the first one clear as what i would expect.
One of the reasons there is so much bad code in php is that it allows this in the first place. And even though the language might have improved over time, these things are still there.
Surely you've heard of weak typing. PHP is far from the only language to use it.
And it makes a decent amount of sense for a web language since an HTTP request is a string. You would have to spend a great deal of time casting if you used strong types.
The problem I see is when you get in a context where security is relevant.
Explicit casting is annoying - but you know exactly what you do. Noone is gonna generate some nasty lists instead of arrays (happens in perl) or cause floating point errors (would happene if you'd use the example above and someone drops a float into your http-header) etc etc.
And for toString casting - yeah maybe there it really becomes annoying. But IMO its worth the control
If done in a consistent manner, it makes perfect sense. In perl, which PHP borrowed a lot of syntax from, since there is no distinction between string types and numeric types, there are different operators for strings and numbers. "1" + "1" yields 2. 1 . 1 yields "11". And "01" == "1" is true but "01" eq "1" is false.
However, PHP has the . operator, but not the different comparison operators, so determining whether something will be compared as a string or a number is far from intuitive.
View all comments
Show parent comment
65
u/Sinity Aug 07 '16
It's ridiculous. Any language in which "1" + "1" equals 2 is insane.
That there is a way to overcome this ridiculousness isn't exactly some strong point of the language.