php / php/php-src

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

Open
#16,029 5 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.