php 如何将布尔值转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2795177/
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
How to Convert Boolean to String
提问by tag
I have a Boolean variable which I want to convert to a string:
我有一个布尔变量,我想将其转换为字符串:
$res = true;
I need the converted value to be of the format: "true" "false", not "0" "1"
我需要转换后的值的格式为: "true" "false", not"0" "1"
$converted_res = "true";
$converted_res = "false";
I've tried:
我试过了:
$converted_res = string($res);
$converted_res = String($res);
But it tells me that stringand Stringare not recognized functions.
How do I convert this Boolean to a string in the format of "true"or "false"in PHP?
但它告诉我,string并且String不是公认的功能。
如何将此布尔值转换为PHP格式"true"或"false"PHP格式的字符串?
回答by hobodave
Simplest solution:
最简单的解决方案:
$converted_res = $res ? 'true' : 'false';
$converted_res = $res ? 'true' : 'false';
回答by Christian Davén
The function var_exportreturns a string representation of a variable, so you could do this:
函数var_export返回一个变量的字符串表示,所以你可以这样做:
var_export($res, true);
The second argument tells the function to return the string instead of echoing it.
第二个参数告诉函数返回字符串而不是回显它。
回答by Freez
Another way to do : json_encode( booleanValue )
另一种做法: json_encode( booleanValue )
echo json_encode(true);  // string "true"
echo json_encode(false); // string "false"
// null !== false
echo json_encode(null);  // string "null"
回答by dev-null-dweller
See var_export
回答by treznik
You use strval() or (string) to convert to string in PHP. However, that does not convert boolean into the actual spelling of "true" or "false" so you must do that by yourself. Here's an example function:
您可以使用 strval() 或 (string) 在 PHP 中转换为字符串。但是,这不会将 boolean 转换为“true”或“false”的实际拼写,因此您必须自己完成。这是一个示例函数:
function strbool($value)
{
    return $value ? 'true' : 'false';
}
echo strbool(false); // "false"
echo strbool(true); // "true"
回答by aleemb
The other solutions here all have caveats (though they address the question at hand). If you are (1) looping over mixed-types or (2) want a generic solution that you can export as a function or include in your utilities, none of the other solutions here will work.
这里的其他解决方案都有警告(尽管它们解决了手头的问题)。如果您 (1) 循环混合类型或 (2) 想要一个通用解决方案,您可以将其导出为函数或包含在您的实用程序中,那么这里的其他解决方案都不起作用。
The simplest and most self-explanatory solution is:
最简单和最不言自明的解决方案是:
// simplest, most-readable
if (is_bool($res) {
    $res = $res ? 'true' : 'false';
}
// same as above but written more tersely
$res = is_bool($res) ? ($res ? 'true' : 'false') : $res;
// Terser still, but completely unnecessary  function call and must be
// commented due to poor readability. What is var_export? What is its
// second arg? Why are we exporting stuff?
$res = is_bool($res) ? var_export($res, 1) : $res;
But most developers reading your code will require a trip to http://php.net/var_exportto understand what the var_exportdoes and what the second param is.
但是大多数阅读您代码的开发人员需要访问http://php.net/var_export以了解它的var_export作用以及第二个参数是什么。
1. var_export
1. var_export
Works for booleaninput but converts everything else to a stringas well.
适用于boolean输入,但也将其他所有内容转换为 a string。
// OK
var_export(false, 1); // 'false'
// OK
var_export(true, 1);  // 'true'
// NOT OK
var_export('', 1);  // '\'\''
// NOT OK
var_export(1, 1);  // '1'
2. ($res) ? 'true' : 'false';
2. ($res) ? 'true' : 'false';
Works for boolean input but converts everything else (ints, strings) to true/false.
适用于布尔输入,但将其他所有内容(整数、字符串)转换为真/假。
// OK
true ? 'true' : 'false' // 'true'
// OK
false ? 'true' : 'false' // 'false'
// NOT OK
'' ? 'true' : 'false' // 'false'
// NOT OK
0 ? 'true' : 'false' // 'false'
3. json_encode()
3. json_encode()
Same issues as var_exportand probably worse since json_encodecannot know if the string truewas intended a string or a boolean.
var_export由于json_encode无法知道字符串true是字符串还是布尔值,因此存在相同的问题,并且可能更糟。
回答by good_evening
Why just don't do like this?:
为什么不这样做?:
if ($res) {
    $converted_res = "true";
}
else {
    $converted_res = "false";
}
回答by dallin
For me, I wanted a string representation unless it was null, in which case I wanted it to remain null.
对我来说,我想要一个字符串表示,除非它是null,在这种情况下我希望它保持null。
The problem with var_export is it converts nullto a string "NULL"and it also converts an empty string to "''", which is undesirable. There was no easy solution that I could find.
var_export 的问题是它转换null为字符串"NULL",并且还将空字符串转换为"''",这是不可取的。我找不到简单的解决方案。
This was the code I finally used:
这是我最终使用的代码:
if (is_bool($val)) $val ? $val = "true" : $val = "false";
else if ($val !== null) $val = (string)$val;
Short and simple and easy to throw in a function too if you prefer.
如果您愿意,也可以简短而简单地放入一个函数中。
回答by T.Todua
This works also for any kind of value:
这也适用于任何类型的值:
$a = true;
echo $a                     // outputs:   1
echo value_To_String( $a )  // outputs:   true
code:
代码:
function valueToString( $value ){ 
    return ( !is_bool( $value ) ?  $value : ($value ? 'true' : 'false' )  ); 
}
回答by asiby
Editedbased on @sebastian-norr suggestion pointing out that the $boolvariable may or may not be a true 0or 1. For example, 2resolves to truewhen running it through a Boolean test in PHP.
根据@sebastian-norr 建议进行编辑,指出该$bool变量可能是也可能不是 true0或1. 例如,在 PHP 中通过布尔测试运行它时2解析为true。
As a solution, I have used type casting to ensure that we convert $boolto 0or 1.
But I have to admit that the simple expression $bool ? 'true' : 'false'is way cleaner.  
作为解决方案,我使用类型转换来确保我们转换$bool为0or 1。
但我不得不承认,简单的表达$bool ? 'true' : 'false'方式更简洁。  
My solution used below should never be used, LOL.
Here is why not...
永远不应使用我在下面使用的解决方案,大声笑。
这就是为什么不...
To avoid repetition, the array containing the string representation of the Boolean can be stored in a constant that can be made available throughout the application.
为了避免重复,包含布尔值字符串表示的数组可以存储在一个常量中,该常量可以在整个应用程序中使用。
// Make this constant available everywhere in the application
const BOOLEANS = ['true', 'false'];
$bool = true;
echo BOOLEANS[(bool)  $bool]; // 'true'
echo BOOLEANS[(bool) !$bool]; // 'false'

