Forgot your password?
typodupeerror

Comment Re:That's because.. (Score 1) 523

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.

Slashdot Top Deals

The reward for working hard is more hard work.

Working...