4 ms·
> What really disturbs me about PHP is its type conversion rules. 1 == '1' or even worse 'foobar' == 0, or '123n' == 123. This is cause for so many subtle bugs.
by wvenable 16y ago
> What really disturbs me about PHP is its type conversion rules. 1 == '1' or even worse 'foobar' == 0, or '123n' == 123. This is cause for so many subtle bugs.
Weird cases like 'foobar' == 0 are extremely rare and 1 == '1' is actually what you want most of the time. I've learned to embrace the type-less nature and use casts when I absolutely want to ensure a particular type. I no longer even consider this a problem.
- pilif 16y agoThe '123n'==123 conversion is the cause for at least 8 very nasty bugs in validating user input in that application I was referring to. And the fact that when comparing integer to strings, PHP prefers to convert the string to compare instead of converting the number (which would be lossless)
- wvenable 16y agoIf you care about numbers in your application, wouldn't the very first test in validating user input would be an is_numeric() call? In my opinion, you have to be doing a lot of other things incorrectly for this to be a real problem.
- pilif 16y agois_numeric doesn't help you if you want to find out whether the value in $foobar is a string that represents an integer or not. The only way to safely do that in PHP is, sadly, a regular expression. is_int checks the type, so is_int('1234') is as false as is_int('123n') or is_int('abc') is_numeric is too permissive as it allows floating point numbers or even hexadecimal values. intval($foobar) == $foobar doesn't work due to intval('123n') == 123 intval($foobar) === $foobar naturally doesn't work because the types don't match. The code in question is the receiving end of some machine generated communication that is supposed to closely follow a specification. During validation if the spec says that it requires a base10 integer encoded in ASCII, then 0xFF isn't permissible just because is_numeric() says so.
- wvenable 16y agoI have this function in my own code. You can adjust it's permissiveness your taste: function is_intval($value) { if (is_integer($value)) return true; if (is_bool($value)) return true; if (!is_numeric($value)) return false; if (is_float($value) && floatval(intval($value)) == $value) return true; if (is_string($value) && strval(intval($value)) == $value) return true; return false; }
- pilif 16y ago... hereby proving my point that PHP's default conversions are a bit... strange.
- wvenable 16y agoI never said it wasn't weird!! I just said that it's not really a practical problem in most cases. Perhaps you do have to be extra careful and I'm just so used to it that I don't give it a second thought.
- prodigal_erik 16y agoYou 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.