【问题标题】:How does this code work correctly?此代码如何正常工作?
【发布时间】:2017-03-06 06:50:43
【问题描述】:

这段代码有什么作用? 我主要看不懂while循环! 主要看不懂$total--

function getNiceFileSize($file, $digits = 2)
{
    if(is_file($file)){
        $filePath = $file;
        if(!realpath($filePath)){
            $filePath = $_SERVER["DOCUMENT_ROOT"] . $filePath;
        }
        $fileSize = filesize($filePath);
        $sizes = array("TB", "GB", "MB", "KB", "B");
        $total = count($sizes);
        while ($total-- && $fileSize > 1024){
            $fileSize /= 1024;
        }
        return round($fileSize, $digits). " " . $sizes[$total];
    }
    return false;
}

【问题讨论】:

    标签: php file-manager


    【解决方案1】:

    $total--$total -= 1 相同,与$total = $total -1 相同。这被称为decrement operator。在查看for 循环时,您可能对增量运算符很熟悉,例如for ($i = 0, $i++, $i < 10)

    循环可以重写为while ($total = $total -1 && $fileSize > 1024),这意味着“虽然$total 仍然是一个真实值(在这种情况下,一个数字> 0)并且$fileSize 是> 1024”。

    【讨论】:

    • 感谢您的有用回答
    • @amir 如果这回答了您的问题,您应该将其标记为已接受可能有相同问题的其他人。
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    相关资源
    最近更新 更多