The short answer is because of PHP's loose comparison "==". The inconsistency is because PHP applies stricter type checking when comparing an enum literal to a variable than when comparing it to a literal value like true. This behavior can be counterintuitive and is a quirk of PHP's type juggling and enum handling. To avoid this you should use strict comparison (===) when working with enums.
Should probably always default to strict comparison unless you've got a clear reason not to. Can always manually cast a value (bool) $truthy before comparison if you need some flexibility. That way it can only get fudged one way and you know exactly where that happens.
2
u/Crapulam Nov 26 '24
So it looks like I've actually ran into a bug/idiosyncrasy of PHP. More details here: https://github.com/php/php-src/issues/16954.
Too bad I'm not too familiar with PHP's internals. I'll let the big boys handle this. :-)
9
u/zovered Nov 25 '24
The short answer is because of PHP's loose comparison "==". The inconsistency is because PHP applies stricter type checking when comparing an enum literal to a variable than when comparing it to a literal value like true. This behavior can be counterintuitive and is a quirk of PHP's type juggling and enum handling. To avoid this you should use strict comparison (===) when working with enums.
5
u/jeroennoten Nov 25 '24
This is really weird
2
u/colshrapnel Nov 26 '24
Pinging /u/Crell