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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址