Skip to content

ext/filter: filter_var_array with FILTER_VALIDATE_INT|FILTER_NULL_ON_FAILURE for an entry flag does not validate. #16993

Closed

Description

@Girgias

Description

The following code:

<?php
$data = [
    'product_id' => '42',
    'component1'  => '10.36',
    'component2'  => '10.36',
];

$filters = [
    'product_id'   => FILTER_VALIDATE_INT,
    'component1'    => FILTER_VALIDATE_INT,
    'component2'    => FILTER_VALIDATE_INT|FILTER_NULL_ON_FAILURE,
    'doesnotexist1' => FILTER_VALIDATE_INT,
    'doesnotexist2' => FILTER_VALIDATE_INT|FILTER_NULL_ON_FAILURE,
];

var_dump(filter_var_array($data, $filters));

Resulted in this output:

array(5) {
  ["product_id"]=>
  int(42)
  ["component1"]=>
  bool(false)
  ["component2"]=>
  string(5) "10.36"
  ["doesnotexist1"]=>
  NULL
  ["doesnotexist2"]=>
  NULL
}

But I expected this output instead:

array(5) {
  ["product_id"]=>
  int(42)
  ["component1"]=>
  bool(false)
  ["component2"]=>
  NULL
  ["doesnotexist1"]=>
  NULL
  ["doesnotexist2"]=>
  NULL
}

PHP Version

PHP 5.4.0 to master

Operating System

No response

Activity

  1. self-assigned this
    on Nov 29, 2024
  2. added theissue type on Feb 5, 2025
  3. removed their assignment
    on Jul 7, 2025
  4. ndossche commented on Jul 19, 2025

    @ndossche
    Member

    I investigated this.
    Correct usage seems to be:

    'component2'    => ['filter' => FILTER_VALIDATE_INT, 'flags' => FILTER_NULL_ON_FAILURE],
    

    and this works as expected.
    I suppose we could at least do with a warning like filter_var emits when having an invalid filter ID, such that users know they're using it "wrong".

  5. Girgias commented on Jul 20, 2025

    @Girgias
    MemberAuthor

    Yeah, I think this makes sense as it is quite confusing of a behaviour

  6. added a commit that references this issue on Sep 8, 2025
    5ae8125
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions