php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41305 BOOLEAN acts strange
Submitted: 2007-05-06 18:13 UTC Modified: 2007-06-17 11:56 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: Se dot Krebs at web dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.2.2 OS: Win32
Private report: No CVE-ID: None
 [2007-05-06 18:13 UTC] Se dot Krebs at web dot de
Description:
------------
FILTER_VALIDATE_BOOLEAN never returns null like its mentioned in the Manual ("otherwise null") without the FILTER_NULL_ON_FAILURE-Option. Even in the Manual is mentioned, that only String-"Booleans" will filterd, but true also returns true.

Maybe without the Option the Manual is a kind of out-of-date, but the fact, that "false" is a Failure, but "true" isnt, seems strange to me.

Reproduce code:
---------------
var_dump( filter_var('false', FILTER_VALIDATE_BOOLEAN));
var_dump( filter_var(false, FILTER_VALIDATE_BOOLEAN));
var_dump( filter_var('true', FILTER_VALIDATE_BOOLEAN));
var_dump( filter_var(true, FILTER_VALIDATE_BOOLEAN));

var_dump( filter_var('false', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // bool(false)
var_dump( filter_var(false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // NULL
var_dump( filter_var('true', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // bool(false)
var_dump( filter_var(true, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); // bool(false)

Expected result:
----------------
// first 4 lines:
bool (false)
NULL
bool (true)
NULL

// last 4 lines the same


Actual result:
--------------
bool (false)
bool (false)
bool (true)
bool (true)

bool (false)
NULL
bool (true)
bool (true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-07 20:53 UTC] tony2001@php.net
Both Ilia & Derick said it's a docu problem.
Reclassified.
 [2007-06-17 11:56 UTC] gwynne@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2010-03-21 18:05 UTC] pravila at alumni dot calpoly dot edu
The filter_var() vs. the filter_input() behave differently when using the 
FILTER_VALIDATE_BOOLEAN filter when the variable/input doesn't exist.

More specifically, it seems as if the FILTER_NULL_ON_FAILURE flag is set 
automatically in the filter_input() function.

(Note: same behavior for filter_var_array() vs. filter_input_array()).

<?php
// example.com/script.php?arg1=yes&arg3=no

// filtering by variable
$var1 = filter_var($_GET["arg1"], FILTER_VALIDATE_BOOLEAN);
$var2 = filter_var($_GET["arg2"], FILTER_VALIDATE_BOOLEAN);
$var3 = filter_var($_GET["arg3"], FILTER_VALIDATE_BOOLEAN);

// filtering by input
$input1 = filter_input(INPUT_GET, "arg1", FILTER_VALIDATE_BOOLEAN);
$input2 = filter_input(INPUT_GET, "arg2", FILTER_VALIDATE_BOOLEAN);
$input3 = filter_input(INPUT_GET, "arg3", FILTER_VALIDATE_BOOLEAN);

// as expected...
var_dump($var1);      // bool(true)
var_dump($var2);      // bool(false)
var_dump($var3);      // bool(false)

// NULL is not an expected return unless the FILTER_NULL_ON_FAILURE flag is 
set...
var_dump($input1);    // bool(true)
var_dump($input2);    // NULL
var_dump($input3);    // bool(false)
?>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 04:01:28 2024 UTC