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

本文翻译自:How to convert string to boolean php

How can I convert string to boolean ? 如何将字符串转换为boolean

$string = 'false';

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

var_dump($test_mode_mail);

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

it returns, 它回来了,

boolean true 布尔值为true

but it should be boolean false . 但它应该是boolean false


#1楼

参考:https://stackoom.com/question/uMEt/如何将字符串转换为布尔值php


#2楼

This method was posted by @lauthiamkok in the comments. 这个方法由@lauthiamkok在评论中发布。 I'm posting it here as an answer to call more attention to it. 我在这里发布它作为回答更多关注它的答案。

Depending on your needs, you should consider using filter_var() with the FILTER_VALIDATE_BOOLEAN flag. 根据您的需要,您应该考虑将filter_var()FILTER_VALIDATE_BOOLEAN标志一起使用。

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(         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

#3楼

Other answers are over complicating things. 其他答案是复杂的事情。 This question is simply logic question. 这个问题只是逻辑问题。 Just get your statement right. 只是让你的陈述正确。

$boolString = 'false';
$result = 'true' === $boolString;

Now your answer will be either 现在你的答案也是

  • false , if the string was 'false' , false ,如果字符串是'false'
  • or true , if your string was 'true' . 或者true ,如果你的字符串是'true'

I have to note that filter_var( $boolString, FILTER_VALIDATE_BOOLEAN ); 我要注意filter_var( $boolString, FILTER_VALIDATE_BOOLEAN ); still will be a better option if you need to have strings like on/yes/1 as alias for true . 如果您需要像on/yes/1这样的字符串作为true别名, 那么仍然是一个更好的选择。


#4楼

You can use settype method too! 你也可以使用settype方法!

SetType($var,"Boolean") Echo $var //see 0 or 1 SetType($ var,“Boolean”)Echo $ var //参见0或1


#5楼

function stringToBool($string){
    return ( mb_strtoupper( trim( $string)) === mb_strtoupper ("true")) ? TRUE : FALSE;
}

or 要么

function stringToBool($string) {
    return filter_var($string, FILTER_VALIDATE_BOOLEAN);
}

#6楼

When working with JSON, I had to send a Boolean value via $_POST . 使用JSON时,我必须通过$_POST发送一个布尔值。 I had a similar problem when I did something like: 当我做了类似的事情时,我遇到了类似的问题:

if ( $_POST['myVar'] == true) {
    // do stuff;
}

In the code above, my Boolean was converted into a JSON string. 在上面的代码中,我的布尔值被转换为JSON字符串。

To overcome this, you can decode the string using json_decode() : 要解决此问题,您可以使用json_decode()解码字符串:

//assume that : $_POST['myVar'] = 'true';
 if( json_decode('true') == true ) { //do your stuff; }

(This should normally work with Boolean values converted to string and sent to the server also by other means, ie, other than using JSON.) (这通常应该使用转换为字符串的布尔值并通过其他方式发送到服务器,即使用JSON以外的其他方式。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值