将布尔值转换为整数值 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20074332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert boolean to integer value php
提问by user2990361
Is there any builtin function for PHP that will take a boolean value and return its integer equivalent? 0 for FALSE, 1 for TRUE?
Of course you can easily create a function to do so, I'm just asking if there is a builtin function inside of PHP. I already tried intval()and casting it to (int)but they didnt work, they return 0 in both cases of TRUE and FALSE.
PHP 是否有任何内置函数将采用布尔值并返回其等效整数?0 代表 FALSE,1 代表 TRUE?当然你可以很容易地创建一个函数来这样做,我只是问PHP内部是否有一个内置函数。我已经尝试过intval()并将其转换为(int)但它们没有用,它们在 TRUE 和 FALSE 的两种情况下都返回 0。
Edit : the problem was that it wasnt really a boolean, it was a string "false", "true", php didnt detect the jquery post that it passed a boolean. problem solved, thanks !
编辑:问题是它不是真正的布尔值,它是一个字符串“false”、“true”,php 没有检测到它传递了一个布尔值的 jquery 帖子。问题解决了,谢谢!
回答by Jérémy Dutheil
http://www.php.net/manual/en/language.types.integer.php
http://www.php.net/manual/en/language.types.integer.php
Normally, simply $myInt = (int)$myBooleanshould work, can you show us your code otherwise ?
通常,$myInt = (int)$myBoolean应该可以正常工作,否则您可以向我们展示您的代码吗?
回答by Dawa
Just add a "+" before your variable like this :
只需在变量前添加一个“+”,如下所示:
$myBool = true;
var_dump(+$myBool);
ouputs: int(1);
回答by Nathan Duckett
If you are unsure of the data type see the example below, it works on strings, integers and booleans.
如果您不确定数据类型,请参见下面的示例,它适用于字符串、整数和布尔值。
<?php
$options = [ TRUE, FALSE, 'true', 'false', 1, 0, '1', '0', 'on', 'off', 'yes', 'no' ];
foreach ( $options as $option ) {
$bool = filter_var( $option, FILTER_VALIDATE_BOOLEAN ); // TRUE or FALSE
print (int) $bool . ' '; // 1 or 0
}
// Outputs: 1 0 1 0 1 0 1 0 1 0 1 0
?>
filter_var( $var, FILTER_VALIDATE_BOOLEAN ) will return bool(true) or bool(false) then simply cast as integer.
filter_var( $var, FILTER_VALIDATE_BOOLEAN ) 将返回 bool(true) 或 bool(false) 然后简单地转换为整数。
(PHP 5 >= 5.2.0, PHP 7)
(PHP 5 >= 5.2.0,PHP 7)
filter_var — Filters a variable with a specified filter
filter_var — 用指定的过滤器过滤变量
回答by Sam Boyne
If you are getting your value in from JSON I.E. Via an AJAX POST, the value will come in as a string (As you have found). A way around this is to compare the true/false string and then cast the bool to an int
如果您通过 AJAX POST 从 JSON IE 获取您的值,则该值将以字符串形式出现(如您所见)。解决此问题的一种方法是比较真/假字符串,然后将 bool 转换为 int
$requestBool = $_REQUEST['bool_value']; //This is 'true' or 'false'
$myInt = (int)($requestBool === 'true');
echo $myInt;
//returns: 0 or 1
回答by Samuel Cook
// cast to Integer
echo (int)true; // 1
echo (int)false; // 0
// get the integer value from argument
echo intval(true); // 1
echo intval(false); // 0
// simply echoing true returns 1
echo true; // 1
// you can even add those values
echo true + true; // 2
回答by user1976610
You can do multiple castings in a single go:
您可以一次进行多次铸造:
$isTest = 'true';
(int)(bool)$isTest
echo $isTest; // this outputs 1
回答by Jokerius
Use type casting (bool):
使用类型转换 (bool):
var_dump((bool) 1); // bool(true)
var_dump((bool) 0); // bool(false)

