Skip to content

Inconsistent behavior of filter_var() with FILTER_REQUIRE_ARRAY and default option for non-array inputs #16029

Open
@rubenferreira97

Description

@rubenferreira97

Description

The following code:

<?php

var_dump(filter_var("a", FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY, 'options' => ['default' => 'Failed']]));
var_dump(filter_var([1], FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY, 'options' => ['default' => 'Failed']]));
var_dump(filter_var(["a"], FILTER_VALIDATE_INT, ['flags' => FILTER_REQUIRE_ARRAY, 'options' => ['default' => 'Failed']]));
var_dump(filter_var("a", FILTER_VALIDATE_INT, ['options' => ['default' => 'Failed']]));

Resulted in this output:

bool(false)
array(1) {[0]=> int(1)}
array(1) {[0]=> string(6) "Failed"}
string(6) "Failed"

But I expected this output instead:

array(1) {[0]=> string(6) "Failed"} or string(6) "Failed"
array(1) {[0]=> int(1)}
array(1) {[0]=> string(6) "Failed"}
string(6) "Failed"

Since the documentation says:

default, value to return if the filter fails

I was expecting to get at least some form of Failed.

PHP Version

PHP 8.3

Operating System

Windows 11

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions