非法数组键

Ron*_*ner 4 php arrays

这个功能

function convert($size) {
  $unit = array(
    'B',
    'KByte',
    'MByte',
    'GByte',
  );
  return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}
Run Code Online (Sandbox Code Playgroud)

有效,但 PHPStorm 说

 第 54 行的非法数组键类型

这意味着 $unit[$i]。

这个功能有什么问题?

lon*_*day 5

floor返回一个浮点数,而不是一个整数。(违反直觉,但确实如此。)浮点数是 PHP 数组中的非法键。

代码工作正常,因为当用作数组中的键时浮点数隐式转换为整数,但我想这就是您在 PHPStorm 中收到通知的原因。