PHP 5.6.12 - 整数返回不正确

标签 php

以前我一直在 Linux 中使用 PHP 5.6.7 开发应用程序,但最近我切换到使用 WAMP 64 位的 Windows 环境并将 PHP 升级到 5.6.12 并遇到了一些问题。一个问题是在后端 PHP 中,我有一个整数数组。当我打印返回到前面将返回数组打印到控制台时,我得到了不同的格式。即:

$permission->fk_org_id = [24053826281537536,24051529749102626,111];
print json_encode($permission->fk_org_id);

将以下内容返回到控制台:

0:24053826281538000
1:24051529749103000
2:111

为什么会这样?

最佳答案

这些数字太大而不适合(32 位)整数。它们将被解释为 float ,参见 documentation :

If PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead.

A float具有大约 14 位有效数字的精度,所以这就是您看到零的原因。

不幸的是,Windows 上的 PHP 不支持 64 位整数,根据 this answer :

On windows x86_64, PHP_INT_MAX is 2147483647. This is because in the underlying c-code, a long is 32 bit.

However, linux on x86_64 uses a 64bit long so PHP_INT_MAX is going to be 9223372036854775807.

关于PHP 5.6.12 - 整数返回不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32095876/

相关文章:

php - zend_PDF drawImage 用于设置位置

php - 使用 Eclipse 将网站部署到外部测试服务器

php - 在 Zend 中重写 session ID 的东西

php - Magento 2 - 设置 :di:compile 中不存在类

php - 以特定间隔合并数组

javascript - 使用 PHP、websocket 和 MySQL 聊天

php - 如何只为登录用户保护页面?

php - 函数参数中的 gmdate() 给出解析错误

php - Symfony3 数据表无法呈现

java - 让 selenium 独立服务器与 phpunit 一起使用时出现问题