 downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net search for in the function list whole site online documentation [en] bug database general mailing list developer mailing list documentation mailing list   Booleans Floating point numbers  view the printer friendly printer friendly [+notes] Brazilian Portuguese Chinese (Simplified) Chinese (Hong Kong Cantonese) Chinese (Traditional) Czech Dutch Finnish French German Hebrew Hungarian Italian Japanese Korean Polish Romanian Russian Slovak Slovenian Spanish Swedish Turkish version of this page Last updated: Thu, 21 Aug 2003

# Integers

An integer is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}.

## Syntax

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 6-1. Integer literals

 ``````
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.

## 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().

### From booleans

FALSE will yield 0 (zero), and TRUE will yield 1 (one).

### From floating point numbers

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!

Warning

Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results.

 ``````

### 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.

User Contributed Notes
Integers
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. ```
13-Feb-2003 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
02-Nov-2002 09: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
15-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! ```
andreaszoellner at gmx dot de
27-Feb-2002 05:30
``` "Die Gr廲e eines Integer-Wertes 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 little-possums dot net dot invalid
15-Jan-2002 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 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.) ``` Booleans Floating point numbers  Last updated: Thu, 21 Aug 2003 show source | credits | sitemap | mirror sites 