du8864 2015-01-14 17:57
浏览 131
已采纳

强制转换为32位整数可能导致截断PHP Propel?

Looking at the source code of Propel (the PHP ORM library), I have found this method inside the propel/propel1/runtime/lib/query/Criteria.php file:

  /**
     * Set offset.
     *
     * @param int $offset An int with the value for offset.  (Note this values is
     *                    cast to a 32bit integer and may result in truncation)
     *
     * @return Criteria Modified Criteria object (for fluent API)
     */
    public function setOffset($offset)
    {
        $this->offset = (int) $offset;

        return $this;
    }

Why in the doc comments they say that the value casted to int may result in truncation??? Isn't the int kept to e.g. 4000000000 in 64 bit environment? Actually, it is, so why this "Note"?

Thanks for the attention!

  • 写回答

1条回答 默认 最新

  • dongyaofu0599 2015-01-14 18:25
    关注

    The maximum and minimum size of integer depends of the build of PHP : 32 or 64 Bits (operating system and processor must also follow)

    For PHP 32-Bits the range is between ]-2147483648, 2147483647[
    For PHP 64-Bits the range is between ]-9223372036854775808, 9223372036854775807[

    My Test (PHP 32-Bits, WINDOWS 7 64-Bits, Intel CORE i3 64-Bits) :

    <?php
    $i = (int)2147483647;
    var_dump($i);
    

    Will ouput :

    int(2147483647)
    

    2nd test (just increment by 1 the last value)

    <?php
        $i = (int)2147483647;
        var_dump($i);
    

    Will ouput :

    int(-2147483648)
    

    Finally : to be sure about the max value of integer in your environment, just print this

    var_dump(PHP_INT_MAX);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题