繁体   English   中英

可以使零为真吗?

[英]Possible to make a zero be true?

我有一个字符串:

$string = '0';

在这样的回声语句中:

echo ($string ? 'true':'false');

我希望'0'true 目前是false

有没有办法调整回声语句 我知道我可以例如在字符串中添加一个空格:

$string = ' 0';

这将是真的 但我希望有一个不需要我调整字符串的解决方案。

有没有办法调整回声语句?

不 - 不编辑 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.

一个更好的主意是添加您自己的全局 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' );
    }
}

?>

像这样使用:

$string = '0';

echo zeroToBoolText( $string );

使用以下条件,您可以更改字符串是否为 0 true..

    if($string === '0')
    {
        $string = true;
    }
    else
    {
        $string = false;
    }

试试看,我觉得你需要这个谢谢。。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM