Search code examples
phpvalidationfilter-var

PHP filter_var bug?


The following code successfully validates the string "0123.250" as a valid float, when it is not. Is this a PHP bug or did I do something wrong?

filter_var('0123.250', FILTER_VALIDATE_FLOAT);

Solution

  • From the PHP documentation on float literals:

    LNUM          [0-9]+
    DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
    EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})
    

    As you can see, there is no restriction for leading zeroes, as indicated by these bits: [0-9]*[\.] and [0-9]+.

    Since the page never once mentions octal, we need to assume that leading zeroes make no difference in the interpretation.

    I assume that the exact same rules are applied for FILTER_VALIDATE_FLOAT.