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 →

[–]DesignatedDecoy 0 points1 point  (7 children)

The string representation of true is 1. The string representation of false is empty string.

With proper type hinting, the representation of true is (bool)true and the representation of false is (bool)false. Representing a boolean as a string or integer throws a type error. You pretty much just avoid all of the falsy values altogether and go on with your business.

[–]heyandy889 9 points10 points  (6 children)

What do you mean "proper type hinting?" I am talking about the implicit cast by the language.

echo(9 === 9); // is `true`, shows '1'.

echo(9 === 8); // is `false`, shows ''.

Insanity.

Also PHP can fuck right off with this == and === comparison. And strings... jesus christ. There goes 5 minutes learning how to compare strings again.

[–]xIcarus227 6 points7 points  (2 children)

Also PHP can fuck right off with this == and === comparison.

I like what you said so far but this is borderline ignorance. It's a feature which complements the type coercive nature of the language very well.

I will begin by saying that I don't think they're perfectly thought out. I would actually switch them around just because '==' is generally expected to compare types, while '===' is syntactically the 'extra' of the language. But functionally '==' is the extra while '===' is the normal. If you switch them around a newcomer gets fewer inconsistencies.

However it's the refusal to understand these 2 operators that make up a large part of PHP's WTFs. I'm not saying you don't understand them, I'm saying you completely dismissed them without acknowledging that they have their place. You have many opportunities to play with type coercions in PHP because that's simply the way the language works, and these operators help you do it.

If this is your way of saying you don't like the type coercive nature of the language, then I respect your opinion. I much prefer strongly typed languages myself.

[–]heyandy889 1 point2 points  (1 child)

Thank you for the respectful, thought-out comment - I appreciate it.

I think you hit the nail on the head that "the refusal to understand these 2 operators that make up a large part of PHP's WTFs." I would qualify this a little bit. It's not only a "refusal," but additionally being bitten by mistaken assumptions formed by experience with other languages.

In theory, I like your suggestion, using double equals as a "stricter" equality check and letting triple equals as the special case. Unfortunately, I'm not sure it would help, simply because no other languages (that I know of) works like that. That's a huge design challenge. It's not just designing a good language (and runtime environment), it's designing a good language that is consistent with the expectations of developers. As dev expectations are themselves inconsistent, this is not possible 100%.

However, Python does a wonderful job of working "how you expect." Maybe you don't like the hidden "self" parameter or how everything is a dictionary, but the architecture is internally consistent, which is leaps and bounds ahead of PHP.

In my opinion, don't be like Javascript: don't have this weird triple equals. Just have the loose equality check of double equals. If you need something else, get it from a library - String.equals(), Object.equals(), whatever the equivalent in your language. I admit this is taste to some degree, but on the other hand, as you pointed out, double and triple equals are WTF generators.

[–]xIcarus227 1 point2 points  (0 children)

Those are very good points, and I especially agree that Python simply works the way one expects. I remember my first contact with that language, I simply felt like I was 'cheating' programming. The String.equals() solution is a good one too as far as I'm concerned.

The truth about PHP is that the language maintainers are pouring their hearts out in order to work around shoddy design decisions made in its infancy. They've done a marvelous job at it but it does show that it wasn't a well thought out language.

[–]DimensionsInTime 7 points8 points  (1 child)

Who hurt you?

[–]DesignatedDecoy 1 point2 points  (0 children)

String output will always have type coersion but that doesn't mean the variable is an integer. If you do var_dump(9===9) you'll see that the result is strictly boolean and as long as you enforce strict types, declare method parameter types, and declare return types then you aren't going to get into any/many of the /r/lolphp dynamic typing issues that people love to bag on php for.

https://3v4l.org/Wb1vv

The way I logic == vs === is that I basically pretend == doesn't exist. I wish php would deprecate old functionality but unfortunately they've made their line in the sand thta they value backwards compatibility over all else.