©
이 문서에서는 PHP 중국어 웹사이트 매뉴얼 풀어 주다
(PHP 4, PHP 5, PHP 7)
is_bool — 检测变量是否是布尔型
$var
)
如果 var
是 boolean 则返回 TRUE
。
Example #1 is_bool() 示例
<?php
$a = false ;
$b = 0 ;
// 因为 $a 是布尔型,所以结果为真
if ( is_bool ( $a )) {
print "Yes, this is a boolean" ;
}
// 因为 $b 不是布尔型,所以结果为非真
if ( is_bool ( $b )) {
print "Yes, this is a boolean" ;
}
?>
参见 is_array() 、 is_float() 、 is_int() 、 is_integer() 、 is_string() 和 is_object() 。
[#1] Michael Smith [2013-11-17 01:17:31]
The function in the last comment also converts the string 'false' to a true boolean. Here's a function that does what I think was intended:
<?php
function toBool($var) {
if (!is_string($var)) return (bool) $var;
switch (strtolower($var)) {
case '1':
case 'true':
case 'on':
case 'yes':
case 'y':
return true;
default:
return false;
}
}
[#2] vincent at lycoops dot be [2012-04-28 12:22:33]
For those of you who think that micro-optimization is important:
It's quicker to strictly compare a value to TRUE and FALSE, than using is_bool(), because it's a function.
<?php
if (is_bool($var)) { // 100% of time
}
if ($var === true || $var === false) { // 50% of time, if $var is true and 75% of time if $var is false.
}
?>
[#3] moty66 at gmail dot com [2009-08-20 14:18:18]
Here is the function which I use when pasring boolean variables from config files
<?php
function boolVal($var) {
switch ($var) {
case $var == true:
case $var == 1:
// case $var == '1': // no need for this, because we used
// $val == 1 not $var === 1
case strtolower($var) == 'true':
case strtolower($var) == 'on':
case strtolower($var) == 'yes':
case strtolower($var) == 'y':
$out = 1;
break;
default: $out = 0;
}
return $out;
}
?>
I am woundering why php does not have a function to do this
Motaz Abuthiab
[#4] sam+nospam at samuellevy dot com [2009-02-01 16:30:00]
Just another little function which doesn't exist yet, but I find mighty useful, especially when working with AJAX and APIs.
<?php
function boolval($in, $strict=false) {
$out = null;
// if not strict, we only have to check if something is false
if (in_array($in,array('false', 'False', 'FALSE', 'no', 'No', 'n', 'N', '0', 'off',
'Off', 'OFF', false, 0, null), true)) {
$out = false;
} else if ($strict) {
// if strict, check the equivalent true values
if (in_array($in,array('true', 'True', 'TRUE', 'yes', 'Yes', 'y', 'Y', '1',
'on', 'On', 'ON', true, 1), true)) {
$out = true;
}
} else {
// not strict? let the regular php bool check figure it out (will
// largely default to true)
$out = ($in?true:false);
}
return $out;
}
?>
It may be pretty inefficient, but it does the job.
[#5] bb at kingdom dot cjb dot net [2007-06-27 02:57:14]
@ emmanuel del grande
You don't need to break when you return...
function bool($var) {
switch (strtolower($var)) {
case ("true"): return true;
case ("false"): return false;
default: die("whatever you want it to tell");
}
}
[#6] kevin at metalaxe dot com [2007-06-05 01:32:45]
@ emanueledelgrande at email dot it
http://us.php.net/manual/en/language.types.type-juggling.php
(bool) or (boolean) is allowed for type casting a variable. No function like intval, etc but the functionality exists.
[#7] emanueledelgrande at email dot it [2007-05-22 09:12:56]
I think there's a hole in the PHP typecasting methods:
you have the (int) function, the (float) function and the (string) function, but no function to force a string variable into the boolean type.
It's obvious that forcing unconditionally the type of variables into arrays and objects is inappropriate, but boolean type is the most basic one for each programming language, that's why I guessed that a (bool) function already existed.
Moreover, with the increasing trend of RSS data streaming, the parsing of an XML string into an object often requires to typecast as boolean values the content of XML tags, normally returned as string by the object method get_content().
I wrote the following function, which also uses a "native PHP style" error message:
<?php
// strings tyecasting as boolean values:
function bool($var) {
switch (strtolower($var)) {
case ("true"):
return true;
break;
case ("false"):
return false;
break;
default:
die("<br />\n<b>Warning:</b> Invalid argument supplied for ".__FUNCTION__." function in <b>".__FILE__."</b> on line <b>".__LINE__."</b>: the argument can contain only 'true' or 'false' values as a string.<br />\n");
}
}
?>
Here it is a small example:
<?php
";
$xmlResponse = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
$xmlResponse .= "<Result>";
$xmlResponse .= "<AuthError>false</AuthError>";
$xmlResponse .= "<TransferStatus>true</TransferStatus>";
$xmlResponse .= "</Result>";
if (! $responseDoc = domxml_open_mem($xmlResponse, DOMXML_LOAD_PARSING, $XmlParsingError)) {
echo "Error while parsing the XML string:<br />".print_r($XmlParsingError, TRUE);
} else {
$ResultNode = $responseDoc->get_elements_by_tagname('Result');
$AuthError = $ResultNode[0]->get_elements_by_tagname('AuthError');
$auth_error = bool($AuthError[0]->get_content());
$TransferStatus = $ResultNode[0]->get_elements_by_tagname('TransferStatus');
$transfer_status = bool($TransferStatus[0]->get_content());
if (! $auth_error) { echo "Auth OK<br />"; } else { echo "Auth error<br />"; }
if ($transfer_status) { echo "Transfer OK<br />"; } else { echo "Transfer error<br />"; }
}
?>
It would be useful this function to be implemented in the core of PHP5.
[#8] rh at richardhoward dot net [2005-05-22 11:17:03]
punkpuke is wrong here; what he means to say is that empty($x) is the opposite of (bool)$x. is_bool($x) returns true where $x === false.