

Integers
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
Syntax
Integers can be specified in decimal (10based), hexadecimal (16based)
or octal (8based) 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 61. Integer literals <?php
$a = 1234; # decimal number
$a = 123; # a negative number
$a = 0123; # octal number (equivalent to 83 decimal)
$a = 0x1A; # hexadecimal number (equivalent to 26 decimal)
?> 

Formally the possible structure for integer literals is:
The size of an integer is platformdependent, although a
maximum value of about two billion is the usual value
(that's 32 bits signed). PHP does not support unsigned
integers.
Integer overflow
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.
Converting to integer
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 typejuggling.
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!
From other types
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.

Matt AKA junkie
29Jul2003 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("/[^09.]/", "", $str);
# Then we make it an integer physically
$i = preg_replace("/\.[09]*$/", "", $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
27Jun2003 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
13Feb2003 07: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
02Nov2002 09:02
Hi,
To easily solve this problem of math operations compile your PHP with enablebcmath 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
15Jun2002 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!
andreaszoellner at gmx dot de
27Feb2002 05:30
"Die Gr廲e eines IntegerWertes ist plattformabh鄚gig, ein Maximalwert von ungef鄣r zwei Billionen ist jedoch lich."
Achtung bei der 鈁ersetzung ins Deutsche!
1 billion auf Englisch = 1 Milliarde auf Deutsch
tim at freeman dot littlepossums dot net dot invalid
15Jan2002 04: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 floatingpoint arithmetic, 6.0/3.0==2.0 exactly. The floatingpoint answer may be 1.999...etc, which would round down to 1.
carl at NOSPAM dot thep dot lu dot se
22Aug2001 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 64bit rather than 32bit all possible values for a 32bit 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.)
 