Skip to content

FILTER_THROW_ON_FAILURE flag? #16710

Closed

Description

@divinity76

Description

a FILTER_THROW_ON_FAILURE alternative to the existing FILTER_NULL_ON_FAILURE would be nice. Could behave pretty much like the existing JSON_THROW_ON_ERROR

usage scenario: replace code like

$b = filter_var($v1, FILTER_VALIDATE_BOOL, FILTER_NULL_ON_FAILURE);
if($b === null) {
    throw new ValueError("failed FILTER_VALIDATE_BOOL");
}
$i = filter_var($v2, FILTER_VALIDATE_INT);
if($i === false) {
    throw new ValueError("failed FILTER_VALIDATE_INT");
}

with

$b = filter_var($v1, FILTER_VALIDATE_BOOL, FILTER_THROW_ON_FAILURE);
$i = filter_var($v2, FILTER_VALIDATE_INT, FILTER_THROW_ON_FAILURE);

Activity

  1. cmb69 commented on Nov 6, 2024

    @cmb69
    Member

    Makes sense, but we need to consider the behavior of filter_var_array().

  2. iluuu1994 commented on Nov 6, 2024

    @iluuu1994
    Member

    This has some overlap with pattern matching, more specifically the as keyword. It's not a full alternative, given that pattern matching doesn't support e-mails, URLs, etc.

  3. DanielEScherzer commented on Mar 25, 2025

    @DanielEScherzer
    Member

    Makes sense, but we need to consider the behavior of filter_var_array().

    I'd say that any time FILTER_NULL_ON_FAILURE would trigger returning null, we would throw, to be consistent

    This probably needs an RFC the same way that https://wiki.php.net/rfc/json_throw_on_error did, I'll add this to my list but I already have two RFCs under discussion at the moment

  4. linked a pull request that will close this issue[RFC] FILTER_THROW_ON_FAILURE #18896on Jun 21, 2025
  5. DanielEScherzer commented on Jun 21, 2025

    @DanielEScherzer
    Member

    @divinity76 I've created a PR for this, can you help provide an example use case that I can put in the RFC?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions