整型

一个 integer 是集合 Z = {..., -2, -1, 0, 1, 2, ...} 中的一个数。

参见任意长度整数(GMP)浮点数任意精度数学库(BCMath)

语法

整型值可以用十进制,十六进制或八进制符号指定,前面可以加上可选的符号(- 或者 +)。

如果用八进制符号,数字前必须加上 0(零),用十六进制符号数字前必须加上 0x

例 11-1. 整数文字表达

<?php
$a
= 1234; // 十进制数
$a = -123; // 一个负数
$a = 0123; // 八进制数(等于十进制的 83)
$a = 0x1A; // 十六进制数(等于十进制的 26)
?>
在字面上,整型变量正式的结构可以为:

decimal     : [1-9][0-9]*
            | 0

hexadecimal : 0[xX][0-9a-fA-F]+

octal       : 0[0-7]+

integer     : [+-]?decimal
            | [+-]?hexadecimal
            | [+-]?octal

整型数的字长和平台有关,尽管通常最大值是大约二十亿(32 位有符号)。PHP 不支持无符号整数。

警告

如果向八进制数传递了一个非法数字(即 8 或 9),则后面其余数字会被忽略。

例 11-2. 八进制数的怪事

<?php
var_dump
(01090); // 010 octal = 8 decimal
?>

整数溢出

如果给定的一个数超出了 integer 的范围,将会被解释为 float。同样如果执行的运算结果超出了 integer 范围,也会返回 float

<?php
$large_number
=  2147483647;
var_dump($large_number);
// 输出为:int(2147483647)

$large_number =  2147483648;
var_dump($large_number);
// 输出为:float(2147483648)

// 同样也适用于十六进制表示的整数:
var_dump( 0x80000000 );
// 输出为:float(2147483648)

$million = 1000000;
$large_number =  50000 * $million;
var_dump($large_number);
// 输出为:float(50000000000)
?>

警告

不幸的是 PHP 中有个 bug,因此当有负数参与时结果并不总是正确。例如当运算 -50000 * $million 时结果是 -429496728。不过当两个运算数都是正数时就没问题。

这个问题已经在 PHP 4.1.0 中解决了。

PHP 中没有整除的运算符。1/2 产生出 float 0.5。可以总是舍弃小数部分,或者使用 round() 函数。

<?php
var_dump
(25/7);         // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7));  // float(4)
?>

转换为整形

要明示地将一个值转换为 integer,用 (int)(integer) 强制转换。不过大多数情况下都不需要强制转换,因为当运算符,函数或流程控制需要一个 integer 参数时,值会自动转换。还可以通过函数 intval() 来将一个值转换成整型。

参见类型戏法

布尔值转换

FALSE 将产生出 0(零),TRUE 将产生出 1(壹)。

浮点数转换

当从浮点数转换成整数时,数字将被取整(丢弃小数位)。

如果浮点数超出了整数范围(通常为 +/- 2.15e+9 = 2^31),则结果不确定,因为没有足够的精度使浮点数给出一个确切的整数结果。在此情况下没有警告,甚至没有任何通知!

【译者注】在 Linux 下返回结果是最小负数(-2147483648),而在 Windows 下返回结果是零(0)。

警告

决不要将未知的分数强制转换为 integer,这样有时会导致意外的结果。

<?php
echo (int) ( (0.1+0.7) * 10 ); // 显示 7!
?>

更多信息见浮点数精度

从其它类型转换

小心

没有定义从其它类型转换为整型的行为。目前的行为和值先转换为布尔值一样。不过不要依靠此行为,因为它会未加通知地改变。


add a note add a note User Contributed Notes
me at troyswanson dot net
12-Jun-2007 10:11
This note applies to machines that are using a 32 bit integer size.  I imagine the same results occur in 64 bit machines as well (with the number 2^63-1).

-2147483648 falls into the range of 32 bit signed integers (0b10000000000000000000000000000000), yet php treats it as a float.  However, -2147483647-1 is treated as an integer.

The following code demonstrates:
<?php
   var_dump
(-2147483648); //float(-2147483648)
  
var_dump(-2147483647 - 1); //int(-2147483648)
?>

Regards
Jacek
10-Mar-2007 12:51
On 64 bits machines max integer value is 0x7fffffffffffffff (9 223 372 036 854 775 807).
09-Mar-2007 03:26
To force the correct usage of 32-bit unsigned integer in some functions, just add '+0'  just before processing them.

for example
echo(dechex("2724838310"));
will print '7FFFFFFF'
but it should print 'A269BBA6'

When adding '+0' php will handle the 32bit unsigned integer
correctly
echo(dechex("2724838310"+0));
will print 'A269BBA6'
popefelix at gmail dot com
21-Dec-2006 02:50
Be careful when using integer conversion to test something to see if it evaluates to a positive integer or not.  You might get unexpected behaviour.

To wit:
<?php
error_reporting
(E_ALL);
require_once
'Date.php';

$date = new Date();
print
"\$date is an instance of " . get_class($date) . "\n";
$date += 0;
print
"\$date is now $date\n";
var_dump($date);

$foo = new foo();
print
"\$foo is an instance of " . get_class($foo) . "\n";
$foo += 0;
print
"\$foo is now $foo\n";
var_dump($foo);

class
foo {
   var
$bar = 0;
   var
$baz = "la lal la";
   var
$bak;

   function
foo() {
      
$bak = 3.14159;
   }
}
?>

After the integer conversion, you might expect both $foo and $date to evaluate to 0.  However, this is not the case:

$date is an instance of Date

Notice: Object of class Date could not be converted to int in /home/kpeters/work/sketches/ObjectSketch.php on line 7
$date is now 1
int(1)
$foo is an instance of foo

Notice: Object of class foo could not be converted to int in /home/kpeters/work/sketches/ObjectSketch.php on line 13
$foo is now 1
int(1)

This is because the objects are first converted to boolean before being converted to int.
rustamabd@gmail-you-know-what
12-Dec-2006 09:42
Be careful with using the modulo operation on big numbers, it will cast a float argument to an int and may return wrong results. For example:
<?php
   $i
= 6887129852;
   echo
"i=$i\n";
   echo
"i%36=".($i%36)."\n";
   echo
"alternative i%36=".($i-floor($i/36)*36)."\n";
?>
Will output:
i=6.88713E+009
i%36=-24
alternative i%36=20
jmw254 at cornell dot edu
25-Aug-2006 05:14
Try this one instead:

function iplongtostring($ip)
{
   $ip=floatval($ip); // otherwise it is capped at 127.255.255.255

   $a=($ip>>24)&255;
   $b=($ip>>16)&255;
   $c=($ip>>8)&255;
   $d=$ip&255;

   return "$a.$b.$c.$d";
}
rickard_cedergren at yahoo dot com
27-Jan-2005 09:15
When doing large subtractions on 32 bit unsigned integers the result sometimes end up negative. My example script converts a IPv4 address represented as a 32 bit unsigned integer to a dotted quad (similar to ip2long()), and adds a "fix" to the operation.

   /**************************
   * int_oct($ip)
   * Convert INTeger rep of IP to octal (dotted quad)
   */
   function int_oct($ip) {

     /* Set variable to float */
     settype($ip, float);

     /* FIX for silly PHP integer syndrome */
     $fix = 0;
     if($ip > 2147483647) $fix = 16777216;

     if(is_numeric($ip)) {
         return(sprintf("%u.%u.%u.%u",
               $ip / 16777216,
               (($ip % 16777216) + $fix) / 65536,
               (($ip % 65536) + $fix / 256) / 256,
               ($ip % 256) + $fix / 256 / 256
               )
     );
     }
     else {
         return('');
     }
   }
23-Dec-2003 06:18
Sometimes you need to parse an unsigned
32 bit integer. Here's a function I 've used:
                                                                              
   function parse_unsigned_int($string) {
       $x = (float)$string;
       if ($x > (float)2147483647)
           $x -= (float)"4294967296";
       return (int)$x;
   }