|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-02-10 05:40 UTC] alex dot howansky at gmail dot com
Description:
------------
FILTER_VALIDATE_INT fails on '01'. I can understand that opinions might be divided on whether this should return 1 or false -- however, in either case, the behavior of FILTER_VALIDATE_FLOAT should match, and it doesn't.
Test script:
---------------
var_dump(filter_var('01', FILTER_VALIDATE_INT));
var_dump(filter_var('01', FILTER_VALIDATE_FLOAT));
Expected result:
----------------
int(1)
double(1)
or:
bool(false)
bool(false)
Actual result:
--------------
bool(false)
double(1)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 21:00:01 2025 UTC |
> Please double-check the documentation I'm unable to find any documentation that contradicts my evaluation or explains this behavior. Can you please link to the specific page you're referring to? I'm confused by the hex/oct comment -- whether or not "01" is interpreted as octal or hex doesn't change the fact that it's still a valid value for an integer. I.e., "01" octal is a valid int, "01" hex is a valid int, and "01" decimal is a valid int. Also, if I plug "01" into any other function that expects an integer, it will work just fine. Consider this: var_dump(abs('01')); var_dump(intval('01')); var_dump(octdec('01')); var_dump(hexdec('01')); var_dump(filter_var('01' + '0.0', FILTER_VALIDATE_INT)); var_dump(filter_var('01', FILTER_VALIDATE_INT)); Returns: int(1) int(1) int(1) int(1) int(1) bool(false) Don't you think that output is rather contradictory?Also, consider this: var_dump(filter_var('0', FILTER_VALIDATE_INT)); var_dump(filter_var('00', FILTER_VALIDATE_INT)); Returns: int(0) bool(false) That's incredibly counter-intuitive.OK, understood. Unfortunately, this leaves an odd hole when working with strings that start with zero and contain an eight or nine. For example, intval() only performs octal validation if you explicitly override the base parameter's default value of 10. As a result, intval('09') gives you exactly what you'd expect -- int(9). Likewise, '09' + 1 works just as expected, in base 10. However, regardless of flags, FILTER_VALIDATE_INT can never return true for string '09'. Given that '09' is perfectly acceptable input for other cases which require an int, this seems inconsistent. Perhaps a new option should be added to this filter, named "base" or similar, which functions like the optional "base" argument of intval().