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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序