PHP printed boolean value is empty, why?

后端 未结 4 833
清歌不尽
清歌不尽 2020-12-01 14:10

I am new to PHP. I am implementing a script and I am puzzled by the following:

$local_rate_filename = $_SERVER[\'DOCUMENT_ROOT\'].\"/ghjr324l.txt\";
$local_r         


        
相关标签:
4条回答
  • 2020-12-01 14:11

    If you wanna check if the file exists when your are not sure of the return type is true/false or 0/1 you could use ===.

    if($local_rates_file_exists === true)
    {
       echo "the file exists";
    }
    else
    {
       echo "the doesnt file exists";
    }
    
    0 讨论(0)
  • 2020-12-01 14:32

    About converting a boolean to a string, the manual actually says:

    A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

    A boolean can always be represented as a 1 or a 0, but that's not what you get when you convert it to a string.

    If you want it to be represented as an integer, cast it to one:

    $intVar = (int) $boolVar;
    
    0 讨论(0)
  • 2020-12-01 14:34

    Be careful when you convert back and forth with boolean, the manual says:

    A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

    So you need to do a:

    echo (int)$local_rates_file_exists."<br>";
    
    0 讨论(0)
  • 2020-12-01 14:35

    The results come from the fact that php implicitly converts bool values to strings if used like in your example. (string)false gives an empty string and (string)true gives '1'. That is consistent with the fact that '' == false and '1' == true.

    0 讨论(0)
提交回复
热议问题