<HTML ><HEAD ><TITLE >Integers</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Types" HREF="language.types.html"><LINK REL="PREVIOUS" TITLE="Booleans" HREF="language.types.boolean.html"><LINK REL="NEXT" TITLE="Floating point numbers" HREF="language.types.float.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.types.boolean.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 7. Types</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.types.float.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.types.integer" ></A >Integers</H1 ><P > An <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A > is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}. </P ><P > See also: <A HREF="ref.gmp.html" >Arbitrary length integers</A > and <A HREF="language.types.float.html" >Floating point numbers</A > </P ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.integer.syntax" ></A >Syntax</H2 ><P > Integers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based) notation, optionally preceded by a sign (- or +). </P ><P > If you use the octal notation, you must precede the number with a <TT CLASS="literal" >0</TT > (zero), to use hexadecimal notation precede the number with <TT CLASS="literal" >0x</TT >. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3302" ></A ><P ><B >Example 7-1. Integer literals</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?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) ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Formally the possible structure for integer literals is: <DIV CLASS="informalexample" ><A NAME="AEN3305" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><?php decimal : [1-9][0-9]* | 0 hexadecimal : 0[xX][0-9a-fA-F]+ octal : 0[0-7]+ integer : [+-]?decimal | [+-]?hexadecimal | [+-]?octal ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > 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. </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.integer.overflow" ></A >Integer overflow</H2 ><P > If you specify a number beyond the bounds of the <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A > type, it will be interpreted as a <A HREF="language.types.float.html" ><B CLASS="type" >float</B ></A > instead. Also, if you perform an operation that results in a number beyond the bounds of the <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A > type, a <A HREF="language.types.float.html" ><B CLASS="type" >float</B ></A > will be returned instead. <DIV CLASS="informalexample" ><A NAME="AEN3314" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $large_number = 2147483647; var_dump($large_number); // output: int(2147483647) $large_number = 2147483648; var_dump($large_number); // output: float(2147483648) // this goes also for hexadecimal specified integers: var_dump( 0x80000000 ); // output: float(2147483648) $million = 1000000; $large_number = 50000 * $million; var_dump($large_number); // output: float(50000000000) ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > <DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > 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 <TT CLASS="literal" >-50000 * $million</TT >, the result will be <TT CLASS="literal" >-429496728</TT >. However, when both operands are positive there is no problem. </P ><P > This is solved in PHP 4.1.0. </P ></TD ></TR ></TABLE ></DIV > </P ><P > There is no integer division operator in PHP. <TT CLASS="literal" >1/2</TT > yields the <A HREF="language.types.float.html" ><B CLASS="type" >float</B ></A > <TT CLASS="literal" >0.5</TT >. You can cast the value to an integer to always round it downwards, or you can use the <A HREF="function.round.html" ><B CLASS="function" >round()</B ></A > function. <DIV CLASS="informalexample" ><A NAME="AEN3326" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php var_dump(25/7); // float(3.5714285714286) var_dump((int) (25/7)); // int(3) var_dump(round(25/7)); // float(4) ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="language.types.integer.casting" ></A >Converting to integer</H2 ><P > To explicitly convert a value to <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >, use either the <TT CLASS="literal" >(int)</TT > or the <TT CLASS="literal" >(integer)</TT > 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 <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A > argument. You can also convert a value to integer with the function <A HREF="function.intval.html" ><B CLASS="function" >intval()</B ></A >. </P ><P > See also <A HREF="language.types.type-juggling.html" >type-juggling</A >. </P ><DIV CLASS="sect3" ><H3 CLASS="sect3" ><A NAME="language.types.integer.casting.from-boolean" ></A >From <A HREF="language.types.boolean.html" >booleans</A ></H3 ><P > <TT CLASS="constant" ><B >FALSE</B ></TT > will yield <TT CLASS="literal" >0</TT > (zero), and <TT CLASS="constant" ><B >TRUE</B ></TT > will yield <TT CLASS="literal" >1</TT > (one). </P ></DIV ><DIV CLASS="sect3" ><H3 CLASS="sect3" ><A NAME="language.types.integer.casting.from-float" ></A >From <A HREF="language.types.float.html" >floating point numbers</A ></H3 ><P > When converting from float to integer, the number will be rounded <SPAN CLASS="emphasis" ><I CLASS="emphasis" >towards zero</I ></SPAN >. </P ><P > If the float is beyond the boundaries of integer (usually <TT CLASS="literal" >+/- 2.15e+9 = 2^31</TT >), 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! </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Never cast an unknown fraction to <A HREF="language.types.integer.html" ><B CLASS="type" >integer</B ></A >, as this can sometimes lead to unexpected results. <DIV CLASS="informalexample" ><A NAME="AEN3356" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php echo (int) ( (0.1+0.7) * 10 ); // echoes 7! ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > See for more information the <A HREF="language.types.float.html#warn.float-precision" >warning about float-precision</A >. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="sect3" ><H3 CLASS="sect3" ><A NAME="language.types.integer.casting.from-string" ></A >From strings</H3 ><P > See <A HREF="language.types.string.html#language.types.string.conversion" >String conversion to numbers</A > </P ></DIV ><DIV CLASS="sect3" ><H3 CLASS="sect3" ><A NAME="language.types.integer.casting.from-other" ></A >From other types</H3 ><P > <DIV CLASS="caution" ><P ></P ><TABLE CLASS="caution" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Caution</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Behaviour of converting to integer is undefined for other types. Currently, the behaviour is the same as if the value was first <A HREF="language.types.boolean.html#language.types.boolean.casting" >converted to boolean</A >. However, do <SPAN CLASS="emphasis" ><I CLASS="emphasis" >not</I ></SPAN > rely on this behaviour, as it can change without notice. </P ></TD ></TR ></TABLE ></DIV > </P ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.types.boolean.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.types.float.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Booleans</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.types.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Floating point numbers</TD ></TR ></TABLE ></DIV ></BODY ></HTML >