dongyutan1703 2017-03-06 06:50
浏览 20

这段代码如何正常工作?

What does this code do ? I can't understand the while loop mainly ! mainly can't understand $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;
}
  • 写回答

1条回答 默认 最新

  • duanli8577 2017-03-06 06:57
    关注

    $total-- is the same as $total -= 1, which is the same as $total = $total -1. This is known as the decrement operator. You are probably familiar with the increment operator when looking at for loops, such as for ($i = 0, $i++, $i < 10).

    The loop can be rewritten as while ($total = $total -1 && $fileSize > 1024), which means "while $total is still a truthy value (in this case, a number > 0) and $fileSize is > 1024".

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看