PHP - 获取 bool 以在 false 时回显 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4948663/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:19:58  来源:igfitidea点击:

PHP - Get bool to echo false when false

phpboolean

提问by Anonymous1

The following code doesn't print out anything:

以下代码不打印任何内容:

$bool_val = (bool)false;
echo $bool_val;

But the following code prints 1:

但是下面的代码打印1

$bool_val = (bool)true;
echo $bool_val;

Is there a better way to print 0or falsewhen $bool_valis falsethan adding an ifstatement?

有没有更好的方式来打印0false$bool_valfalse不是增加一个if声明?

回答by Dan Grossman

echo $bool_val ? 'true' : 'false';

Or if you only want output when it's false:

或者,如果您只想在它为 false 时输出:

echo !$bool_val ? 'false' : '';

回答by Saad

This is the easiest way to do this:

这是最简单的方法:

$text = var_export($bool_value,true);
echo $text;

or

或者

var_export($bool_value)

If the second argument is not true, it will output the result directly.

如果第二个参数不为真,则直接输出结果。

回答by Ignacio Vazquez-Abrams

No, since the other option is modifying the Zend engine, and one would be hard-pressed to call that a "better way".

不,因为另一种选择是修改 Zend 引擎,很难称其为“更好的方法”。

Edit:

编辑:

If you really wanted to, you could use an array:

如果你真的想,你可以使用一个数组:

$boolarray = Array(false => 'false', true => 'true');
echo $boolarray[false];

回答by serdar.sanri

This will print out boolean value as it is, instead of 1/0.

这将按原样打印出布尔值,而不是 1/0。

    $bool = false;

    echo json_encode($bool);   //false

回答by Yanick Rochon

Try converting your boolean to an integer?

尝试将您的布尔值转换为整数?

 echo (int)$bool_val;

回答by akond

I like this one to print that out

我喜欢这个打印出来

var_dump ($var);

回答by Jon Surrell

var_exportprovides the desired functionality.

var_export提供所需的功能。

This will alwaysprint a value rather than printing nothing for nullor false. var_exportprints a PHP representation of the argument it's passed, the output could be copy/pasted back into PHP.

这将始终打印一个值,而不是为nullor打印任何内容falsevar_export打印它传递的参数的 PHP 表示,输出可以复制/粘贴回 PHP。

var_export(true);    // true
var_export(false);   // false
var_export(1);       // 1
var_export(0);       // 0
var_export(null);    // NULL
var_export('true');  // 'true'   <-- note the quotes
var_export('false'); // 'false'

If you want to print strings "true"or "false", you can cast to a boolean as below, but beware of the peculiarities:

如果要打印字符串"true"or "false",可以按如下方式转换为布尔值,但要注意其特殊性:

var_export((bool) true);   // true
var_export((bool) false);  // false
var_export((bool) 1);      // true
var_export((bool) 0);      // false
var_export((bool) '');     // false
var_export((bool) 'true'); // true
var_export((bool) null);   // false

// !! CAREFUL WITH CASTING !!
var_export((bool) 'false'); // true
var_export((bool) '0');     // false

回答by David Oliver

This gives 0or 1:

这给出01

intval($bool_val);

PHP Manual: intval function

PHP 手册:intval 函数

回答by mohamedeimo

echo(var_export($var)); 

When $varis boolean variable, trueor falsewill be printed out.

when$var是布尔变量,trueorfalse会被打印出来。

回答by nuala

The %boption of sprintf()will convert a boolean to an integer:

%b的选项的sprintf()将一个布尔值转换为整数:

echo sprintf("False will print as %b", false); //False will print as 0
echo sprintf("True will print as %b", true); //True will print as 1

If you're not familiar with it: You can give this function an arbitrary amount of parameters while the first one should be your ouput string spiced with replacement strings like %bor %sfor general string replacement.

如果你不熟悉的话:你可以给这个函数的参数的任意量,同时第一个应该是您与替换字符串像五香输出中的字符串%b%s通用字符串替换。

Each pattern will be replaced by the argument in order:

每个模式将按顺序替换为参数:

echo sprintf("<h1>%s</h1><p>%s<br/>%s</p>", "Neat Headline", "First Line in the paragraph", "My last words before this demo is over");