Inconsistent behavior of `filter_var()` with `FILTER_REQUIRE_ARRAY` and `default` option for non-array inputs
Open
Nobody has claimed this yet.
Bug
Extension: filter
Status: Needs Triage
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the four filter_var() examples from the issue on PHP 8.3 and compare the outputs with the documented default behavior. Trace the filter_var() implementation and its existing tests for FILTER_REQUIRE_ARRAY and the default option; done means the chosen behavior is consistent for scalar and array inputs and covered by regression tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100