php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80202 request "y" and "n" support for FILTER_VALIDATE_BOOLEAN
Submitted: 2020-10-08 11:21 UTC Modified: -
From: divinity76 at gmail dot com Assigned:
Status: Open Package: Unknown/Other Function
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
30 - 12 = ?
Subscribe to this entry?

 
 [2020-10-08 11:21 UTC] divinity76 at gmail dot com
Description:
------------
i wish for FILTER_VALIDATE_BOOLEAN to support "y" (for bool(true)) and "n" (for bool(false))


in shell scripts, "y" and "n" often mean "yes" and "no" for yes/no questions in shell scripts, as an example, the GNU Coreutils "yes" program (usually located at /usr/bin/yes , https://man7.org/linux/man-pages/man1/yes.1.html )
will spam "y\n" indefinitely, to be used in shell scripts that ask for yes/no questions, for example "yes | sensors-detect" to tell lm-sensors' sensors-detect to scan for all supported sensors,

so when writing php shell scripts asking for yes/no, it's a shame that FILTER_VALIDATE_BOOLEAN doesn't support y/n, now one has to write something like
<?php
    $confirm = readline("confirm: ");
    $confirm = ((strtolower($confirm)) === "y" ? true : ((strtolower($confirm)) === "n" ? false : (filter_var($confirm, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE))));
    if($confirm === null){
       "error didn't understand blah blah";
    }
?>

instead of
<?php
    $confirm = readline("confirm: ");
    $confirm = filter_var($confirm, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
    if($confirm === null){
       "error didn't understand blah blah";
    }
?>


Test script:
---------------
<?php
var_dump(
filter_var('y', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
filter_var('n', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
);

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


Actual result:
--------------
NULL
NULL


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC