php / php/doc-en

FILTER_VALIDATE_BOOLEAN returns wrong result for null

Open
#3,801 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

  • #3797 by @DanielEScherzer — closed without merging
bug Extension: filter
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Description

The following code:

<?php
var_dump(filter_var(null, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));

Resulted in this output:

bool(false)

But I expected this output instead:

null

Documentation states

Returns true for "1", "true", "on" and "yes". Returns false otherwise.
If FILTER_NULL_ON_FAILURE is set, false is returned only for "0", "false", "off", "no", and "", and null is returned for all non-boolean values.

So the expected result should return null as null is not '0', 'false', 'off' or ''.

PHP Version

php 8.0.28

Operating System

Debian

Contributor guide

No contributing guide indexed for this repository

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 reviewing the FILTER_VALIDATE_BOOLEAN documentation and the reported filter_var example. Compare the documented FILTER_NULL_ON_FAILURE behavior with the PHP 8.0.28 output; done means the relevant documentation or example accurately reflects the confirmed behavior. A linked pull request and the existing discussion should also be checked before starting.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.