note 101180 added to language.types.boolean
| From: | oscar at oveas dot com | Date: | Wed, 01 Dec 2010 11:17:59 +0000 |
| Subject: | note 101180 added to language.types.boolean | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174510@lists.php.net to get a copy of this message | ||
Dunno if someone else posted this solution already, but if not, here's a useful and function to
convert strings to strict booleans.
Note this one only checks for string and defaults to the PHP (boolean) cast where e.g. -1 returns
true, but you easily add some elseifs for other datatypes.
<?php
function toStrictBoolean ($_val, $_trueValues = array('yes', 'y',
'true'), $_forceLowercase = true)
{
if (is_string($_val)) {
return (in_array(
($_forceLowercase?strtolower($_val):$_val)
, $_trueValues)
);
} else {
return (boolean) $_val;
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 212.121.110.171
----
Manual Page -- http://www.php.net/manual/en/language.types.boolean.php
Edit -- https://master.php.net/note/edit/101180
Del: integrated -- https://master.php.net/note/delete/101180/integrated
Del: useless -- https://master.php.net/note/delete/101180/useless
Del: bad code -- https://master.php.net/note/delete/101180/bad+code
Del: spam -- https://master.php.net/note/delete/101180/spam
Del: non-english -- https://master.php.net/note/delete/101180/non-english
Del: in docs -- https://master.php.net/note/delete/101180/in+docs
Del: other reasons-- https://master.php.net/note/delete/101180
Reject -- https://master.php.net/note/reject/101180
Search -- https://master.php.net/manage/user-notes.php