|
|
 |
An integer is a number of the set
Z = {..., -2, -1, 0, 1, 2, ...}.
See also:
Arbitrary length integer / GMP,
Floating point numbers, and
Arbitrary precision / BCMath
Integers can be specified in decimal (10-based), hexadecimal (16-based)
or octal (8-based) notation, optionally preceded by a sign (- or +).
If you use the octal notation, you must precede the number with a
0 (zero), to use hexadecimal notation precede
the number with 0x.
Example 11-1. Integer literals |
<?php
$a = 1234; $a = -123; $a = 0123; $a = 0x1A; ?>
|
|
Formally the possible structure for integer literals is:
The size of an integer is platform-dependent, although a
maximum value of about two billion is the usual value
(that's 32 bits signed). PHP does not support unsigned
integers.
| Warning |
If an invalid digit is passed to octal integer (i.e. 8 or 9), the rest
of the number is ignored.
Example 11-2. Octal weirdness |
<?php
var_dump(01090); ?>
|
|
|
If you specify a number beyond the bounds of the integer
type, it will be interpreted as a float instead. Also, if
you perform an operation that results in a number beyond the bounds of
the integer type, a float will be returned
instead.
| Warning |
Unfortunately, there was a bug in PHP so that this
does not always work correctly when there are negative numbers
involved. For example: when you do -50000 *
$million, the result will be
-429496728. However, when both operands are
positive there is no problem.
This is solved in PHP 4.1.0.
|
There is no integer division operator in PHP.
1/2 yields the float
0.5. You can cast the value to
an integer to always round it downwards, or you can
use the round() function.
To explicitly convert a value to integer, use either
the (int) or the (integer) cast.
However, in most cases you do not need to use the cast, since a value
will be automatically converted if an operator, function or
control structure requires an integer argument.
You can also convert a value to integer with the function
intval().
See also type-juggling.
FALSE will yield
0 (zero), and TRUE
will yield 1 (one).
When converting from float to integer, the number will
be rounded towards zero.
If the float is beyond the boundaries of integer
(usually +/- 2.15e+9 = 2^31),
the result is undefined, since the float hasn't
got enough precision to give an exact integer result.
No warning, not even a notice will be issued in this
case!
| Caution |
Behaviour of converting to integer is undefined for other
types. Currently, the behaviour is the same as if the value
was first converted to boolean. However, do
not rely on this behaviour, as it can
change without notice.
|
User Contributed Notes
Integers
rickard_cedergren at yahoo dot com
27-Jan-2005 03: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('');
}
}
dave at burtonsys dot com
04-Jun-2004 08:42
Matt, you need to also preserve the minus sign.
Negative integers are integers, too.
-Dave
23-Dec-2003 12: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;
}
Matt AKA junkie
29-Jul-2003 05:46
Let's say we're in the situation, let's say a from a form, and the what you want from them is an integer. To be nice though, we're going to fix their error of adding anything other than an integer instead of telling them to do it.
function make_int($str) {
# Let's make sure this isn't being done for no reason
if (gettype($str)!="integer") {
# First we'll make it into a "float" (physically speaking) if not already one
$i = preg_replace("/[^0-9.]/", "", $str);
# Then we make it an integer physically
$i = preg_replace("/\.[0-9]*$/", "", $i);
# Finally, we define the integer as an integer
$i = (int) $i;
return $i;
} else {
return $str;
}}
Just to reiterate, this will take the "string" (not the type) inputted, and make it an integer.
swiftouch at yahoo dot com
27-Jun-2003 07:08
If you were to accidentally precede a number with: +- or -+ ...as in:
$yada = +-1234;
will default to a negative number
or:
$yada = -+1234;
The (-) negative will always take precedence.
This is, of course, if you precede a number with -+ or +- by accident.
adam at forsalebyowner dot com
12-Feb-2003 05:19
Whoah. The lack of support for > 32 bit integers is really a pain for some things. Anyway, for bitwise operations requiring a > 32 bit number:
($big42bitInteger & $someSmallInteger)
You can move to mysql or some other db if you're using one anyway:
select id,($big42bitInteger & $someSmallInteger) as isBitwiseAND FROM someTable;
if ($row->isBitwiseAND > 0) {
do stuff;
}
laercio at gcsnet dot com dot br
02-Nov-2002 07:02
Hi,
To easily solve this problem of math operations compile your PHP with --enable-bcmath and use its set of intruction:
bcadd, bccomp, bcdiv, bcmod, bcmul, bcpow, bcscale, bcsqrt, bcsub.
No more flotpoint problems.
The intruction can be found on this manual.
Laercio Fortes
kennyc at horizondigital dot com
14-Jun-2002 08:57
ONELINE LEADING ZERO ADDER: I kept getting frustrated by php's weak number formatting functions and using the recursive leading zero adders that I've seen in other people code, so here's my quick and easy oneliner for adding leading zero's to any number:
Two digit space:
substr($num+100,1);
0=00
5=05
23=23
3.14=03.14
Four digit space:
substr($num+10000,1);
0=0000
5=0005
23=0023
256=0256
4096=4096
98.6=0098.6
You may want to check first to be sure that your number dosen't already exceed it's character size!
tim at freeman dot little-possums dot net dot invalid
15-Jan-2002 02:20
> there is no risk of rounding errors when doing (int)(n1/n2).
This is not quite true. Although both n1 and n2 can be represented exactly in a float, their quotient often cannot be. It is not even guaranteed that for floating-point arithmetic, 6.0/3.0==2.0 exactly. The floating-point answer may be 1.999...etc, which would round down to 1.
carl at NOSPAM dot thep dot lu dot se
22-Aug-2001 07:51
One note about integer division:
PHP of course has the << and >> operators, so as long as you need to do integer division by a power of 2 you can just shift to the right.
However, as the floating point numbers are 64-bit rather than 32-bit all possible values for a 32-bit int can be represented exactly by a float, so there is no risk of rounding errors when doing (int)(n1/n2).
The overhead of converting to float and back can probably be ignored for an interpreted language like PHP, unless the engine is insanely optimized. (Testing confirms that $a/$b takes only marginally longer than $a>>$b and $a*$b for integers $a and $b.)
| |