如何将字符串转换为布尔值?

$string = 'false';

$test_mode_mail = settype($string, 'boolean');

var_dump($test_mode_mail);

if($test_mode_mail) echo 'test mode is on.';

它返回,

布尔真

但它应该是布尔值为false。


当前回答

如果你的“布尔”变量来自一个全局数组,如$_POST和$_GET,你可以使用filter_input()过滤函数。

例如POST:

$isSleeping  = filter_input(INPUT_POST, 'is_sleeping',  FILTER_VALIDATE_BOOLEAN);

如果你的“布尔”变量来自其他来源,你可以使用filter_var()过滤函数。

例子:

filter_var('true', FILTER_VALIDATE_BOOLEAN); // true

其他回答

最简单的方法是:

$str = 'TRUE';

$boolean = strtolower($str) == 'true' ? true : false;

var_dump($boolean);

这样做,你可以循环一系列'true', 'true', 'false'或'false',并将字符串值获取为布尔值。

我对wordpress shortcode属性感到困惑,我决定写一个自定义函数来处理所有可能性。也许对某些人有用:

function stringToBool($str){
    if($str === 'true' || $str === 'TRUE' || $str === 'True' || $str === 'on' || $str === 'On' || $str === 'ON'){
        $str = true;
    }else{
        $str = false;
    }
    return $str;
}
stringToBool($atts['onOrNot']);

使用preg_match()编辑显示工作解决方案;根据包含true的字符串返回布尔值true或false。与其他答案相比,这可能比较重,但可以很容易地调整以适合布尔值需要的任何字符串。

$test_mode_mail = 'false';      
$test_mode_mail = 'true'; 
$test_mode_mail = 'true is not just a perception.';

$test_mode_mail = gettype($test_mode_mail) !== 'boolean' ? (preg_match("/true/i", $test_mode_mail) === 1 ? true:false):$test_mode_mail;

echo ($test_mode_mail === true ? 'true':'false')." ".gettype($test_mode_mail)." ".$test_mode_mail."<br>"; 

字符串“false”实际上被PHP视为“TRUE”值。 文件说:

To explicitly convert a value to boolean, use the (bool) or (boolean) casts. However, in most cases the cast is unnecessary, since a value will be automatically converted if an operator, function or control structure requires a boolean argument. See also Type Juggling. When converting to boolean, the following values are considered FALSE: the boolean FALSE itself the integer 0 (zero) the float 0.0 (zero) the empty string, and the string "0" an array with zero elements an object with zero member variables (PHP 4 only) the special type NULL (including unset variables) SimpleXML objects created from empty tags Every other value is considered TRUE (including any resource).

所以如果你这样做:

$bool = (boolean)"False";

or

$test = "false";
$bool = settype($test, 'boolean');

在这两种情况下,$bool将为TRUE。所以你必须手动操作,就像GordonM建议的那样。

这个方法是由@lauthiamkok在评论中发布的。我把它贴在这里作为一个答案,以引起更多的关注。

根据需要,您应该考虑使用带有FILTER_VALIDATE_BOOLEAN标志的filter_var()。

filter_var(    true, FILTER_VALIDATE_BOOLEAN); // true
filter_var(    'true', FILTER_VALIDATE_BOOLEAN); // true
filter_var(         1, FILTER_VALIDATE_BOOLEAN); // true
filter_var(       '1', FILTER_VALIDATE_BOOLEAN); // true
filter_var(      'on', FILTER_VALIDATE_BOOLEAN); // true
filter_var(     'yes', FILTER_VALIDATE_BOOLEAN); // true

filter_var(   false, FILTER_VALIDATE_BOOLEAN); // false
filter_var(   'false', FILTER_VALIDATE_BOOLEAN); // false
filter_var(         0, FILTER_VALIDATE_BOOLEAN); // false
filter_var(       '0', FILTER_VALIDATE_BOOLEAN); // false
filter_var(     'off', FILTER_VALIDATE_BOOLEAN); // false
filter_var(      'no', FILTER_VALIDATE_BOOLEAN); // false
filter_var('asdfasdf', FILTER_VALIDATE_BOOLEAN); // false
filter_var(        '', FILTER_VALIDATE_BOOLEAN); // false
filter_var(      null, FILTER_VALIDATE_BOOLEAN); // false