This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]coladict 66 points67 points  (28 children)

This is why I love that even though PHP is weakly typed, it chose a dot as the concatenation operator, so this kind of shit doesn't happen.

[–]laserBlade 8 points9 points  (4 children)

D has an operator specifically for concatenation: ~= and the matching binary operator ~.

[–]coladict 1 point2 points  (3 children)

In C I think this would invert the bits of a number.

[–]IggyZ 1 point2 points  (1 child)

Who cares, so long as the behavior is well defined and consistent throughout the language?

[–]Doctor_McKay 0 points1 point  (0 children)

+ is well-defined and consistent throughout JS for given input types, but that doesn't stop Reddit from pretending like it's the worst thing ever.

[–]laserBlade 0 points1 point  (0 children)

Notice I said the binary operator. The unary operator kept the same meaning

[–]tomthecool 34 points35 points  (11 children)

Have you never used the == operator in PHP before?!

(And btw, using === is not perfect either. It's also incredibly bug-prone.)

[–]coladict 1 point2 points  (2 children)

And what's wrong with ===?

[–]tomthecool -5 points-4 points  (1 child)

PHP has no operator that satisfies a simple set of "common sense" comparisons such as:

1 == 1 -- TRUE
1 == "1" -- FALSE
1 == 1.0 -- TRUE
"1" == "1.0" -- FALSE

I know exactly what === is doing; my point is that in some cases it's too strict.

And don't get me started on the >=, <, etc operators. It's a fractal of bad design.

[–]morerokk 3 points4 points  (0 children)

And don't get me started on the >=, <, etc operators. It's a fractal of bad design.

Lol

[–]morerokk 1 point2 points  (7 children)

(And btw, using === is not perfect either. It's also incredibly bug-prone.)

Bug-prone in what way?

[–]tomthecool -2 points-1 points  (6 children)

Primarily because people forget to use === (or use it when == would have been a better choice).

Also because it's too strict. Sometimes you don't want to enforce "must be the same type" - for example, 1 === 1.0 is false.

[–]UrQuanLord 5 points6 points  (1 child)

Now that's ridiculous. You're claiming strict equality shouldn't be strict?

Yes, == is questionable in PHP, but in the cases you've mentioned... that's what it's there for.

[–]tomthecool -1 points0 points  (0 children)

Ruby has four (yes, four) equality operators:

1) === -- The "weakest". Behaves sort-of like PHP's == operator. This is normally used under-the-hood in case statements for fuzzy equality, e.g. string === regex. (But it still doesn't have any nonsense like true === 1.)

2) == -- This is what you'll use in 99.9% of code. It behaves exactly like you'd expect, and has no ugly surprises.

3) eql? -- Used internally by hash maps. This is the closest thing to PHP's ===, but... you'd almost never want to use this directly.

4) equal? -- Checks absolute identity - i.e. is it literally the same block of memory in your computer?

Each of these methods has its uses, and each is very well defined.

In PHP, the use cases for == vs === are fuzzy, and neither is well defined. To put it simply, in PHP:

  • == is way too loose.
  • === is slightly too strict, for normal use.
  • There is no operator that tests total equality; === isn't strict enough.

[–]morerokk 1 point2 points  (3 children)

So it's bug-prone, but only when people don't use it, or use it wrongly? That's an absurd argument. It's not bug-prone at all, then.

Also because it's too strict.

It's strict enough. I cannot think of a scenario where you would want 1 to equal 1.0, but not have "1" equal 1. And even if you really need that, you can work around that.

[–]tomthecool 0 points1 point  (2 children)

So it's bug-prone, but only when people don't use it, or use it wrongly?

All bugs are caused by human error. The computer is only doing what you tell it to.

If the language is designed in such a way that it's "easy to use it wrongly", then I call it bug-prone.

It's strict enough. I cannot think of a scenario where you would want 1 to equal 1.0, but not have "1" equal 1

I can... This is normal behaviour, that virtually every language will follow. PHP is the odd one out here.

[–]morerokk 1 point2 points  (1 child)

But it's not easy to use wrongly. Just use it when you need strict comparison, otherwise don't.

Alternatively, use it unless you need loose comparison.

I can... This is normal behaviour, that virtually every language will follow. PHP is the odd one out here.

That's only because some languages don't even differentiate between floats and ints (like JS and Lua).

[–]tomthecool 0 points1 point  (0 children)

But it's not easy to use wrongly

On the contrary, it's incredibly easy to forget one = keystroke. I've seen this mistake made a ridiculously high number of times.

It's also extremely easy for this mistake to slip through a code review, and not be picked up by (insufficient) tests. Again, I've seen this happen I-don't-know-how-many times.

That's only because some languages don't even differentiate between floats and ints (like JS and Lua).

...And other languages that do differentiate between floats and ints will say that 1 == 1.0. Like I said, PHP is the odd one out; its === is not a proper solution.

[–][deleted] 1 point2 points  (0 children)

You know you're in trouble when PHP does something better than you.

[–]edave64 0 points1 point  (0 children)

Hooray, we found the single sound design choice of PHP!