Search code examples
phpphpstorm

PhpStorm code inspection returns "Illegal array key type float"


In PhpStorm I get warning about illegal array key type but I can't figure out what illegal about $size[$factor].

Illegal array key type float

This is my code:

    $size       = array(' kB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB');
    $factor     = round((strlen($kbytes) - 1) / 3);
    $sizereturn = sprintf("%.{$decimals}f", $kbytes / pow(1024, $factor)) . @$size[$factor];
    $sizereturn = str_replace('.', ',', $sizereturn);

Solution

  • round() function returns float.

    Valid keys for an array element access should be integers and strings.

    Try casting it to int first, e.g.

    $factor     = (int)round((strlen($kbytes) - 1) / 3);