【临时解决】php int 32 64,关于64位PHP仍然使用32位数字的问题「建议收藏」

大家好,我们又见面了,我是全栈君。

首先,我们了解到有两个常量:PHP_INT_MAX 和 PHP_INT_SIZE。根据PHP官方手册,整型数的字长取决于平台。尽管通常在32位有符号系统下的最大值约为二十亿,64位平台下的最大值通常约为9E18。PHP不支持无符号整数。整型值的字长可以通过常量PHP_INT_SIZE来表示,自PHP 4.4.0和PHP 5.0.5起,最大值可以通过常量PHP_INT_MAX来表示。

然而,当我们在Windows上使用64位PHP(版本5.6.4和5.2.17)时,PHP_INT_SIZE为4,PHP_INT_MAX为2^31-1。相比之下,如果在Linux上使用64位PHP,PHP_INT_SIZE为8,PHP_INT_MAX为2^63-1。PHP Bugs官方也有一条相关的BUG报告:

https://www.php.cn/link/69d355e07534e5c2f03bac0f464bd231

立即学习“PHP免费学习笔记(深入)”;

以下是我的输出:

D:\Website\IIS>cat int.php
echo 'PHP_VERSION = ' . PHP_VERSION . "\n";
echo 'PHP_INT_MAX = ' . PHP_INT_MAX . "\n";
echo 'PHP_INT_SIZE = ' . PHP_INT_SIZE . "\n";

D:\Website\IIS>php int.php
PHP_VERSION = 5.6.4
PHP_INT_MAX = 2147483647
PHP_INT_SIZE = 4

D:\Website\IIS>php --version
PHP 5.6.4 (cli) (built: Dec 17 2014 13:23:31)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.7, Copyright (c) 2002-2015, by Derick Rethans

为什么会这样呢?

我们可以在PHP的main/main.c中找到答案:

REGISTER_MAIN_LONG_CONSTANT("PHP_INT_SIZE", sizeof(long), CONST_PERSISTENT | CONST_CS);

这表明PHP_INT_SIZE的大小与C语言的long类型长度相关,应该是编译器的问题。

我们知道,PHP在Windows上使用VC++编译器。我们来测试一下。

首先是x86编译器的结果:

Z:\>cl /EHsc int.cpp && int
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.60610.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.
int.cpp
Microsoft (R) Incremental Linker Version 11.00.60610.1
Copyright (C) Microsoft Corporation.  All rights reserved.
/out:int.exe
int.obj
int (size = 4) max = 2147483647
long (size = 4) max = 2147483647
llong (size = 8) max = 9223372036854775807

然后使用64位编译器编译:

Z:\>cl /EHsc int.cpp && int
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.60610.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.
int.cpp
Microsoft (R) Incremental Linker Version 11.00.60610.1
Copyright (C) Microsoft Corporation.  All rights reserved.
/out:int.exe
int.obj
int (size = 4) max = 2147483647
long (size = 4) max = 2147483647
llong (size = 8) max = 9223372036854775807

接下来,我们使用g++编译器编译:

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: /cygdrive/i/szsz/tmpp/cygwin64/gcc/gcc-4.8.3-2/src/gcc-4.8.3/configure --srcdir=/cygdrive/i/szsz/tmpp/cygwin64/gcc/gcc-4.8.3-2/src/gcc-4.8.3 --prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin --libexecdir=/usr/libexec --datadir=/usr/share --localstatedir=/var --sysconfdir=/etc --libdir=/usr/lib --datarootdir=/usr/share --docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C --build=x86_64-pc-cygwin --host=x86_64-pc-cygwin --target=x86_64-pc-cygwin --without-libiconv-prefix --without-libintl-prefix --enable-shared --enable-shared-libgcc --enable-static --enable-version-specific-runtime-libs --enable-bootstrap --disable-__cxa_atexit --with-dwarf2 --with-tune=generic --enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite --enable-threads=posix --enable-libatomic --enable-libgomp --disable-libitm --enable-libquadmath --enable-libquadmath-support --enable-libssp --enable-libada --enable-libgcj-sublibs --disable-java-awt --disable-symvers --with-ecj-jar=/usr/share/java/ecj.jar --with-gnu-ld --with-gnu-as --with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix --without-libintl-prefix --with-system-zlib --libexecdir=/usr/lib
Thread model: posix
gcc version 4.8.3 (GCC)
sx@zsx-pc /cygdrive/z$ g++ int.cpp -o int & ./int
[1] 25508
int (size = 4) max = 2147483647
long (size = 8) max = 9223372036854775807
llong (size = 8) max = 9223372036854775807

通过比较这两个编译器long的size和max,我们可以理解为什么在旧版本的PHP中INT_MAX的值不同。

因此,在PHP7中,这个问题应该不会再出现。

在Windows系统下,64位PHP中的PHP_INT_MAX为32位,许多在Windows环境下开发的人可能遇到过一个问题:从数据库中取出的int值比64位PHP中的int值大,导致使用intval过滤后,数值不准确。

在MySQL中,int分为有符号和无符号类型,有符号的最大值为:2147483647,无符号的最大值为:4294967295。

理论上,64位PHP的PHP_INT_SIZE应为:8,PHP_INT_MAX应为:9223372036854775807。

实际上,在Linux系统下的64位PHP中,PHP_INT_MAX的值为:9223372036854775807,而在Windows系统下,64位PHP的PHP_INT_MAX的值为:2147483647。

通过查看PHP源码中PHP_INT_SIZE的定义:

REGISTER_MAIN_LONG_CONSTANT("PHP_INT_SIZE", sizeof(long), CONST_PERSISTENT | CONST_CS);

可以看到PHP_INT_SIZE的大小与C语言的long类型长度相同。

PHP在Windows平台使用VC++编译,在Linux平台使用g++编译。查阅了关于这两个编译器在64位环境下的资料:

VC++(64位) g++(64位)
int 4 4
long 4 8
long long 8 8

原来在64位编译环境中,VC++和g++的long类型长度是有区别的。因此,Windows下64位PHP的int值会比MySQL中无符号的int值小。

在不升级PHP版本的情况下,如何解决这个问题呢?可以临时修改项目:

在框架中将return PHP_INT_MAX > 2147483647;改为return PHP_INT_MAX +1 > 2147483647;

发布者:全栈程序员栈长,转载请注明出处:https://www.php.cn/link/e2701537f72184bb152aa9d26a93ecca 原文链接:https://www.php.cn/link/c8377ad2a50fb65de28b11cfc628d75c