PHP 布尔值到字符串并进行修改 &一个条件
当 echo
布尔值(true 或 false)时,PHP 将其转换为 1
或
并显示它。例如:
$x = true; echo $x; //displays: 1
$x = false; echo $x; //displays: <nothing>
我的问题:如果变量是布尔值,是否有一个 PHP 函数(如果不是如何编码的话)可以准确显示“true”或“false”(而不是 1 或什么都没有) 否则就按照 PHP 正常显示的方式显示。
When echo
ing a boolean (true or false), PHP converts it to 1
or <nothing>
and displays it. e.g.:
$x = true; echo $x; //displays: 1
$x = false; echo $x; //displays: <nothing>
My Question: Is there a PHP function (if not how to code it) which can display exactly "true" or "false" (and not 1 or nothing), if a variable is a boolean otherwise just display as PHP would normally display it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我一直在想这样做,但需要在没有函数的情况下内联它,所以我只是使用:
不是最容易阅读的,但可以完成工作!
I've just been looking to do this but needed to to it inline without a function so I just used :
Not the easiest to read but gets the job done!
是的,它可以很容易地编码如下:
输出:
Yes, it can be easily coded like this:
This outputs:
我使用
headers_sent()
只是作为 BOOLEAN 变量。I used
headers_sent()
just as a BOOLEAN variable.我遇到了这个问题,发现这个问题,答案并不完全是我需要的,所以我解决了这个:
array_pop(array_keys(array('FALSE' => false,'TRUE' => true), [bool] ))
来简单地获取 [bool] 的打印(回显)字符串值,将 [bool] 替换为您知道是 bool 的变量。
我认为这可能会帮助其他发现这个问题的人......
I had this issue, found this question and the answer was not quite what I needed, so I worked out this:
array_pop(array_keys(array('FALSE' => false,'TRUE' => true), [bool]))
to simply get a printed (echoed) string value for a [bool], replacing [bool] with a variable you know to be a bool.
Thought it might help others who find this question....