Weakly typed means variables have no type to speak or, or they are every type.
This is one definition of "weakly typed" that I hear a lot. Except most folks tack on the fact values *do* have types (otherwise, you're looking at assembly where everything has type bit string or brainf*ck or forth or what have you). The other definition, which I believe is apt, is that the language provides many implicit conversions.
In PHP "5" and 5 are the same thing, there is no difference between them as far as the language is concerned. It is a string and an int, that's the very nature of a weakly typed language. That is what makes them "easy" all conversions are automagic.
That means in PHP you can do "5" + 5 and the answer should be "10" and 10. It is impossible to know when I give you "5" + 5 + "5" in PHP whether I meant concat or add or some combination.
Here is the difficulty. I think you are conflating two different ideas:
a) Variables do not have types, but values do. b) "Lots of" implicit conversions exist in the language*.
For example, "5" and 5 definitely *are* different as far as the language is concerned. To convince yourself of this, try evaluating is_string(5) and is_int("5"). You can easily be convinced that is_string(5) will always output FALSE, and is_string("5") will always output TRUE. Thus values must have types associated with them. However, the output of is_string($foo) depends on the value of $foo (not the variable).
With that said, it is correct that PHP will make "easy conversions" "automagically". But this illustrates that PHP definitely is converting a value of some type A to a value of some different type B. And most importantly, PHP knows if a value has some type A or B.
Okay, now that my pendantic ramblings are out of the way, this is significant because under the hood, PHP can detect if $foo or "5" or whatever is a string. Thus it can make the result of $foo + $bar + $baz be a concatenation if any of $foo, $bar, or $baz are strings, and it can make it an addition if none are strings (or it can add until it finds a string, or whatever).
This can happen because PHP is a typed language, and it doesn't particularly matter if PHP was dynamically typed, statically typed, "weakly typed", "strongly typed", "duck typed" or whatever.
So anyways, it *is* possible to use + as both a concatenation and addition operator in a meaningful way. However, to use it the way I have suggested would break existing code. Furthermore, since I haven't really followed PHP development for a while, I can't say if the community would even welcome such a change (if no code needed to be updated).
* Exactly how many implicit conversions the language must offer to you before you call it weakly typed I do not know. Java offers some implicit conversions, but people call it strongly typed. Ruby offers a lot, but not as many as PHP, and so people argue about whether or not it is weakly typed. Scala offers programmer defined implicit conversions, so I do not even know what to think about weak typing in that case, which is why the term means so little to me.
I don't know what you mean by "weakly typed language" as it has several different meanings*, but I think being or not being weakly typed doesn't have anything to do with it.
The fact is that PHP automatically and forcefully converts operands into the correct type with the '+' operator (is this what you mean by weakly typed?). That is, "5" + 5 = 10, so all operands are converted to numerics.
Incidentally, Java, will do this the other way, as "5" + 5 = "55". All operands to + get converted to String ("5" + obj is the same as "5" + obj.toString). Curiously, nobody is quick to call Java a weakly typed language.
The exception, in Java's case, is the rule that + will always concatenate:
Any time a String is encountered, Java will decide it is a concatenation operation (never addition). If you want to use the + operator for addition, you'll have to make sure all the operands are of some numeric type.
For PHP, the original developers decided that it would be much more useful to always be able to add instead of concatenate. For example:
"b" + 5 = 5// PHP; b gets converted to numeric value of 0
Of course, they could have the opposite rule so that if a numeric is present, then you must add; otherwise the operation is a concatenation. My opinion (and theirs probably) is that this rule would be terribly confusing, so they used the . operator for concatenation.
On the flip side, my opinion is also that having "b" + 5 evaluate to 5 is also terribly confusing! There was nothing stopping them from making PHP behave like Java in this case (and still have PHP be "weakly typed", whatever that means).
Unfortunately, it's way too late now, as there is already a ton of code exploiting this messy design decision.
* Some people take weakly typed to mean types are associated with values instead of variables, for example.
Weakly typed means variables have no type to speak or, or they are every type.
This is one definition of "weakly typed" that I hear a lot. Except most folks tack on the fact values *do* have types (otherwise, you're looking at assembly where everything has type bit string or brainf*ck or forth or what have you). The other definition, which I believe is apt, is that the language provides many implicit conversions.
In PHP "5" and 5 are the same thing, there is no difference between them as far as the language is concerned. It is a string and an int, that's the very nature of a weakly typed language. That is what makes them "easy" all conversions are automagic.
That means in PHP you can do "5" + 5 and the answer should be "10" and 10. It is impossible to know when I give you "5" + 5 + "5" in PHP whether I meant concat or add or some combination.
Here is the difficulty. I think you are conflating two different ideas:
a) Variables do not have types, but values do.
b) "Lots of" implicit conversions exist in the language*.
For example, "5" and 5 definitely *are* different as far as the language is concerned. To convince yourself of this, try evaluating is_string(5) and is_int("5"). You can easily be convinced that is_string(5) will always output FALSE, and is_string("5") will always output TRUE. Thus values must have types associated with them. However, the output of is_string($foo) depends on the value of $foo (not the variable).
With that said, it is correct that PHP will make "easy conversions" "automagically". But this illustrates that PHP definitely is converting a value of some type A to a value of some different type B. And most importantly, PHP knows if a value has some type A or B.
Okay, now that my pendantic ramblings are out of the way, this is significant because under the hood, PHP can detect if $foo or "5" or whatever is a string. Thus it can make the result of $foo + $bar + $baz be a concatenation if any of $foo, $bar, or $baz are strings, and it can make it an addition if none are strings (or it can add until it finds a string, or whatever).
This can happen because PHP is a typed language, and it doesn't particularly matter if PHP was dynamically typed, statically typed, "weakly typed", "strongly typed", "duck typed" or whatever.
So anyways, it *is* possible to use + as both a concatenation and addition operator in a meaningful way. However, to use it the way I have suggested would break existing code. Furthermore, since I haven't really followed PHP development for a while, I can't say if the community would even welcome such a change (if no code needed to be updated).
* Exactly how many implicit conversions the language must offer to you before you call it weakly typed I do not know. Java offers some implicit conversions, but people call it strongly typed. Ruby offers a lot, but not as many as PHP, and so people argue about whether or not it is weakly typed. Scala offers programmer defined implicit conversions, so I do not even know what to think about weak typing in that case, which is why the term means so little to me.
I don't know what you mean by "weakly typed language" as it has several different meanings*, but I think being or not being weakly typed doesn't have anything to do with it.
The fact is that PHP automatically and forcefully converts operands into the correct type with the '+' operator (is this what you mean by weakly typed?). That is, "5" + 5 = 10, so all operands are converted to numerics.
Incidentally, Java, will do this the other way, as "5" + 5 = "55". All operands to + get converted to String ("5" + obj is the same as "5" + obj.toString). Curiously, nobody is quick to call Java a weakly typed language.
The exception, in Java's case, is the rule that + will always concatenate:
5 + "5" = 55 // Java // Java // Java
"5" + 5 = 55
5 + 5 + 5 + "5" = 155
Any time a String is encountered, Java will decide it is a concatenation operation (never addition). If you want to use the + operator for addition, you'll have to make sure all the operands are of some numeric type.
For PHP, the original developers decided that it would be much more useful to always be able to add instead of concatenate. For example:
"b" + 5 = 5 // PHP; b gets converted to numeric value of 0
Of course, they could have the opposite rule so that if a numeric is present, then you must add; otherwise the operation is a concatenation. My opinion (and theirs probably) is that this rule would be terribly confusing, so they used the . operator for concatenation.
On the flip side, my opinion is also that having "b" + 5 evaluate to 5 is also terribly confusing! There was nothing stopping them from making PHP behave like Java in this case (and still have PHP be "weakly typed", whatever that means).
Unfortunately, it's way too late now, as there is already a ton of code exploiting this messy design decision.
* Some people take weakly typed to mean types are associated with values instead of variables, for example.