dt250827 2016-06-30 02:06
浏览 89
已采纳

PHP注意:字符串偏移转换发生在第251和258行

i get php notice with this

$code = "";
while ($id > $length - 1) {
    // determine the value of the next higher character
    // in the short code should be and prepend
    $code = self::$chars[fmod($id, $length)] . $code; //<-- line 251
    // reset $id to remaining value to be converted
    $id = floor($id / $length);
}

// remaining value of $id is less than the length of
// self::$chars
$code = self::$chars[$id] . $code; //<-- line 258

return $code;

The NOTICE code is this part:

$code = self::$chars[fmod($id, $length)] . $code; 
$code = self::$chars[$id] . $code; 

How to fix it? I can’t find it, please your help all.. :)

  • 写回答

1条回答 默认 最新

  • douzen1896 2016-06-30 02:14
    关注

    Using intval should fix it:

    ..$chars[intval(fmod($id, $length))]..
    

    That is caused because fmod returns a float, and it casts it to integer. See this example:

    $a = 'abcdef';
    $b = $a[1.0];
    echo $b;
    

    And the output:

    E_NOTICE : type 8 -- String offset cast occurred -- at line 3
    b


    If you think about it (at least in this case), you can either be at position 1, or at position 2, for example. You can't be at position 1,5. So when you try to access something float, it automatically casts the value to integer and lets you know that it did that.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?