5 ms·
We could debate that for a long time, but my point was different: There's no sane function in today's PHP that parses ints and handles errors (defaulting to 0 d
by dvdkon 4y ago
We could debate that for a long time, but my point was different: There's no sane function in today's PHP that parses ints and handles errors (defaulting to 0 doesn't count).
- Y-bar 4y ago> defaulting to 0 doesn't count Why this arbitrary restriction? A forced cast of a string to integer is always guaranteed to produce an integer.
- dvdkon 4y agoAnd a forced cast is therefore a bad way to generally parse a user-provided string to int.
- samus 4y agoAnd how are we supposed to distinguish that from a legit 0?
- Y-bar 4y agois_numeric() has been available since forever, and is intended for exactly the thing you appear to want to do. https://www.php.net/manual/en/function.is-numeric.php https://www.php.net/manual/en/function.is-numeric.php
- deathanatos 4y agoAt this point you're writing the parser that ships in most language's standard libraries. And, if you naïvely use the suggested function for the purpose you've suggested it for, e.g., like, if(!is_numeric($input)) { error(); } else { return (int)$input; } …you'll parse a lot of strings that still aren't valid integers. (E.g., "2e-2" is "numeric" and would thus parse.)
- nucleardog 4y ago/** * Cast to int, fail if not a valid integer * @throws \UnexpectedValueException * @param mixed $thing * @return int */ function as_int($thing): int { if (!filter_var($thing, \FILTER_VALIDATE_INT)) throw new \UnexpectedValueException("Value is not an integer."); return (int)$thing; } While, yeah, it'd be nice if there were something like this built in... it's a couple lines of code. If that's your major complaint, I'd be happy to whip up a whole suite of proper functions to cast to primitives while throwing errors on invalid values since it's just swapping FILTER_VALIDATE_INT for the various types.
- dvdkon 4y agoI think this illustrates my problem with PHP well: There are ways to do common tasks properly, but they're often hidden, obscure or basically require creating your own "stdlib extension", as seen here (I only heard about filter_var from this discussion, and I specifically searched for string conversion functions). Your function proves my point: It looks correct, but actually breaks for as_int("0"), since filter_var with FILTER_VALIDATE_INT doesn't acutally validate the int, but parse it, and zero is falsy.
- tored 4y agoAbove usage of filter_var is wrong, there is no need to cast $thing to int, filter_var already returns the filtered int or false on failure. The correct usage is therefore /** * Cast to int, fail if not a valid integer * @throws \UnexpectedValueException * @param mixed $thing * @return int */ function as_int($thing): int { $int = filter_var($thing, \FILTER_VALIDATE_INT); if ($int === false) { throw new \UnexpectedValueException("Value is not an integer."); } return $int; } Thus the critique against PHP that is lacks a proper integer parsing function is simply not true, filter_var has been part of PHP since 5.2 released in 2006, over 16 years ago. filter_var works as advertised, because I use it almost every time i write PHP and that is to mostly to parse integers but sometimes also for other types. And I don't even wrap it with my own as_int function, using it inline with an simple if-check afterwards is actually cleaner. And if you prefer that filter_var returns null on failure instead of false you can simply add FILTER_NULL_ON_FAILURE as the third argument to filter_var. And this is the recurring thing with PHP critique, vast majority of the time it just in imagination because nobody bothers to look it up. https://www.php.net/manual/en/function.filter-var.php https://www.php.net/manual/en/function.filter-var.php