如何将字符串转换为布尔值?
$string = 'false';
$test_mode_mail = settype($string, 'boolean');
var_dump($test_mode_mail);
if($test_mode_mail) echo 'test mode is on.';
它返回,
布尔真
但它应该是布尔值为false。
如何将字符串转换为布尔值?
$string = 'false';
$test_mode_mail = settype($string, 'boolean');
var_dump($test_mode_mail);
if($test_mode_mail) echo 'test mode is on.';
它返回,
布尔真
但它应该是布尔值为false。
当前回答
字符串“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建议的那样。
其他回答
当使用JSON时,我必须通过$_POST发送一个布尔值。当我做类似的事情时,我也遇到了类似的问题:
if ( $_POST['myVar'] == true) {
// do stuff;
}
在上面的代码中,我的布尔值被转换为JSON字符串。
为了克服这个问题,你可以使用json_decode()解码字符串:
//assume that : $_POST['myVar'] = 'true';
if( json_decode('true') == true ) { //do your stuff; }
(这通常适用于布尔值转换为字符串并通过其他方式发送到服务器,即,除了使用JSON。)
@GordonM的回答很好。 但如果$string已经为真(即,字符串不是字符串而是布尔true),它将失败…这似乎不合逻辑。
延伸一下他的回答,我会用:
$test_mode_mail = ($string === 'true' OR $string === true));
(boolean)json_decode(strtolower($string))
它处理$string的所有可能变体
'true' => true
'True' => true
'1' => true
'false' => false
'False' => false
'0' => false
'foo' => false
'' => false
这个方法是由@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
你应该能够使用(bool)强制转换为布尔型,但我不确定没有检查这是否适用于字符串“true”和“false”。
不过这可能值得一试
$myBool = (bool)"False";
if ($myBool) {
//do something
}
值得注意的是,当将以下内容放入其中时,将计算为布尔值False
if()
布尔值FALSE本身 整数0(零) 浮点数0.0(零) 空字符串和字符串"0" 零元素数组 一个没有成员变量的对象(仅适用于PHP 4) 特殊类型NULL(包括未设置的变量) 从空标记创建的SimpleXML对象
其他的都是真。
如下所述: http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting