[英]Possible to make a zero be true?
I have a string:我有一个字符串:
$string = '0';
And in an echo statement like this:在这样的回声语句中:
echo ($string ? 'true':'false');
I want the '0'
to be true .我希望'0'
为true 。 It currently is false .目前是false 。
Is there a way to adjust the echo statement ?有没有办法调整回声语句? I know I can for example add a space in the string:我知道我可以例如在字符串中添加一个空格:
$string = ' 0';
And it will be true .这将是真的。 But I am hoping for a solution that doesn't require me adjusting the string.但我希望有一个不需要我调整字符串的解决方案。
Is there a way to adjust the echo statement?有没有办法调整回声语句?
No - not without editing PHP's source code and rebuilding it yourself.不 - 不编辑 PHP 的源代码并自己重建它是不行的。 The echo
statement in PHP has 20-year-old well-documented behaviour - it would be foolish to make your PHP environment incompatible with the rest of the PHP ecosystem just to save yourself a few keystrokes. The echo
statement in PHP has 20-year-old well-documented behaviour - it would be foolish to make your PHP environment incompatible with the rest of the PHP ecosystem just to save yourself a few keystrokes.
A better idea is just to add your own global function:一个更好的主意是添加您自己的全局 function:
<?php
function zeroToBoolText( $str ) {
if( !is_string( $str ) ) die( "Argument is not a string." );
if( $str === '0' ) {
return 'true';
}
else {
return ( $str ? 'true' : 'false' );
}
}
?>
Used like so:像这样使用:
$string = '0';
echo zeroToBoolText( $string );
Using the below condition you can change if string is 0 true..使用以下条件,您可以更改字符串是否为 0 true..
if($string === '0')
{
$string = true;
}
else
{
$string = false;
}
Try it, i think you need this one thank you..试试看,我觉得你需要这个谢谢。。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.