7 ms·
You want '0' == 0 and ' 0' == 0. But you also get '0' == false array('0' => 'x') == array(false => 'x') ' 0' == true array(' 0' => 'x') != array(true =
by prodigal_erik 16y ago
You want '0' == 0 and ' 0' == 0. But you also get
'0' == false
array('0' => 'x') == array(false => 'x')
' 0' == true
array(' 0' => 'x') != array(true => 'x')
which isn't even an equivalence relation. I have to try this or look it up every time I run into it, because it doesn't make any damn sense.
- wvenable 16y agoFrom the documentation: "A key may be either an integer or a string" So your boolean true is converted to the integer 1 and your boolean false is converted to the integer 0. Dump those arrays and you'll see they don't contain boolean keys. Then it's just a simple matter of '0' == 0 and ' 0' != 1. It's all perfectly logical.
- prodigal_erik 16y agoYeah, I can stare at the docs and work out the consequences of the ridiculous design decisions they committed to (such as array keys using an equivalence relation while equals does not, and some but not all strings being true yet converting to zero). But for me it's just too blatantly wrong to keep a good grasp on, and the experience of trying to resembles the Book from Anathem: > crafted and refined over many centuries to be nonsensical, maddening, and pointless[....] The punishment lay in knowing that you were putting all of that effort into letting a kind of intellectual poison infiltrate your brain to its very roots.
- wvenable 16y agoI don't bother keeping a good grasp on it. The less you try and reason about it, the more likely it works as intended. You can concoct lots of scenarios where the type system in PHP breaks down yet in the average well-designed code those sorts of problems are rare. Your example highlights some weirdness in the type system, but that could would never exist in actual product.