is_bool — 检测变量是否是布尔型
如果 var 是 boolean 则返回 TRUE。 Example #1 is_bool() 示例描述
bool is_bool ( mixed $var )<?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";
}
?>
If you want to convert a string representation of a boolean ,like "yes", "on", "true", "false",etc..., to an actual boolean, you can try this:
<?php
$myString = "On";
$b = filter_var($myString, FILTER_VALIDATE_BOOLEAN);
?>
This will return TRUE for "1", "true", "on" and "yes". Returns FALSE otherwise.
<?php
/**
* Checks a variable if it is true or false, humanlike.
* We account for values as 'on', '1', '' and so on.
* Finally, for some reare occurencies we account with
* crazy logic to fit some arrays and objects.
*
* @author Kim Steinhaug, <kim@steinhaug.com>
* @param mixed $var, the variable to check
*
* Example:
* $test = 'true';
* if(_bool($test)){ echo 'true'; } else { echo 'false'; }
*/
function _bool($var){
if(is_bool($var)){
return $var;
} else if($var === NULL || $var === 'NULL' || $var === 'null'){
return false;
} else if(is_string($var)){
$var = trim($var);
if($var=='false'){ return false;
} else if($var=='true'){ return true;
} else if($var=='no'){ return false;
} else if($var=='yes'){ return true;
} else if($var=='off'){ return false;
} else if($var=='on'){ return true;
} else if($var==''){ return false;
} else if(ctype_digit($var)){
if((int) $var)
return true;
else
return false;
} else { return true; }
} else if(ctype_digit((string) $var)){
if((int) $var)
return true;
else
return false;
} else if(is_array($var)){
if(count($var))
return true;
else
return false;
} else if(is_object($var)){
return true;// No reason to (bool) an object, we assume OK for crazy logic
} else {
return true;// Whatever came though must be something, OK for crazy logic
}
}
?>
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
Just another little function which doesn't exist yet, but I find mighty useful, especially when working with AJAX and APIs.
<?php
/** Checks a variable to see if it should be considered a boolean true or false.
* Also takes into account some text-based representations of true of false,
* such as 'false','N','yes','on','off', etc.
* @author Samuel Levy <sam+nospam@samuellevy.com>
* @param mixed $in The variable to check
* @param bool $strict If set to false, consider everything that is not false to
* be true.
* @return bool The boolean equivalent or null
*/
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.
@ 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");
}
}
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛