<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Integers</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.boolean.html">Booleans</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.float.html">Floating point numbers</a></div> <div class="up"><a href="language.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.types.integer" class="sect1"> <h2 class="title">Integers</h2> <p class="simpara"> An <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> is a number of the set ℤ = {..., -2, -1, 0, 1, 2, ...}. </p> <p class="para"> See also: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <a href="ref.gmp.html" class="link">Arbitrary length integer / GMP</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="language.types.float.html" class="link">Floating point numbers</a> </span> </li> <li class="listitem"> <span class="simpara"> <a href="ref.bc.html" class="link">Arbitrary precision / BCMath</a> </span> </li> </ul> <div class="sect2" id="language.types.integer.syntax"> <h3 class="title">Syntax</h3> <p class="simpara"> <span class="type"><a href="language.types.integer.html" class="type Integer">Integer</a></span>s can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8) or binary (base 2) notation, optionally preceded by a sign (- or +). </p> <p class="para"> Binary <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> literals are available since PHP 5.4.0. </p> <p class="para"> To use octal notation, precede the number with a <em>0</em> (zero). To use hexadecimal notation precede the number with <em>0x</em>. To use binary notation precede the number with <em>0b</em>. </p> <div class="example" id="example-69"> <p><strong>Example #1 Integer literals</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1234</span><span style="color: #007700">; </span><span style="color: #FF8000">// decimal number<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= -</span><span style="color: #0000BB">123</span><span style="color: #007700">; </span><span style="color: #FF8000">// a negative number<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">0123</span><span style="color: #007700">; </span><span style="color: #FF8000">// octal number (equivalent to 83 decimal)<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">0x1A</span><span style="color: #007700">; </span><span style="color: #FF8000">// hexadecimal number (equivalent to 26 decimal)<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">0b11111111</span><span style="color: #007700">; </span><span style="color: #FF8000">// binary number (equivalent to 255 decimal)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> Formally, the structure for <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> literals is: </p> <div class="informalexample"> <div class="example-contents"> <div class="cdata"><pre> decimal : [1-9][0-9]* | 0 hexadecimal : 0[xX][0-9a-fA-F]+ octal : 0[0-7]+ binary : 0b[01]+ integer : [+-]?decimal | [+-]?hexadecimal | [+-]?octal | [+-]?binary </pre></div> </div> </div> <p class="para"> The size of an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>s. <span class="type"><a href="language.types.integer.html" class="type Integer">Integer</a></span> size can be determined using the constant <strong><code>PHP_INT_SIZE</code></strong>, and maximum value using the constant <strong><code>PHP_INT_MAX</code></strong> since PHP 4.4.0 and PHP 5.0.5. </p> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> If an invalid digit is given in an octal <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> (i.e. 8 or 9), the rest of the number is ignored. </p> <div class="example" id="example-70"> <p><strong>Example #2 Octal weirdness</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">01090</span><span style="color: #007700">); </span><span style="color: #FF8000">// 010 octal = 8 decimal<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> </div> <div class="sect2" id="language.types.integer.overflow"> <h3 class="title">Integer overflow</h3> <p class="para"> If PHP encounters a number beyond the bounds of the <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type, it will be interpreted as a <span class="type"><a href="language.types.float.html" class="type float">float</a></span> instead. Also, an operation which results in a number beyond the bounds of the <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> type will return a <span class="type"><a href="language.types.float.html" class="type float">float</a></span> instead. </p> <div class="example" id="example-71"> <p><strong>Example #3 Integer overflow on a 32-bit system</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">2147483647</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">); </span><span style="color: #FF8000">// int(2147483647)<br /><br /></span><span style="color: #0000BB">$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">2147483648</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">); </span><span style="color: #FF8000">// float(2147483648)<br /><br /></span><span style="color: #0000BB">$million </span><span style="color: #007700">= </span><span style="color: #0000BB">1000000</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">50000 </span><span style="color: #007700">* </span><span style="color: #0000BB">$million</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">); </span><span style="color: #FF8000">// float(50000000000)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-72"> <p><strong>Example #4 Integer overflow on a 64-bit system</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">9223372036854775807</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">); </span><span style="color: #FF8000">// int(9223372036854775807)<br /><br /></span><span style="color: #0000BB">$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">9223372036854775808</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">); </span><span style="color: #FF8000">// float(9.2233720368548E+18)<br /><br /></span><span style="color: #0000BB">$million </span><span style="color: #007700">= </span><span style="color: #0000BB">1000000</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">50000000000000 </span><span style="color: #007700">* </span><span style="color: #0000BB">$million</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$large_number</span><span style="color: #007700">); </span><span style="color: #FF8000">// float(5.0E+19)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> There is no <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> division operator in PHP. <em>1/2</em> yields the <span class="type"><a href="language.types.float.html" class="type float">float</a></span> <em>0.5</em>. The value can be casted to an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> to round it downwards, or the <span class="function"><a href="function.round.html" class="function">round()</a></span> function provides finer control over rounding. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">25</span><span style="color: #007700">/</span><span style="color: #0000BB">7</span><span style="color: #007700">); </span><span style="color: #FF8000">// float(3.5714285714286) <br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((int) (</span><span style="color: #0000BB">25</span><span style="color: #007700">/</span><span style="color: #0000BB">7</span><span style="color: #007700">)); </span><span style="color: #FF8000">// int(3)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">round</span><span style="color: #007700">(</span><span style="color: #0000BB">25</span><span style="color: #007700">/</span><span style="color: #0000BB">7</span><span style="color: #007700">)); </span><span style="color: #FF8000">// float(4) <br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="sect2" id="language.types.integer.casting"> <h3 class="title">Converting to integer</h3> <p class="simpara"> To explicitly convert a value to <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>, use either the <em>(int)</em> or <em>(integer)</em> casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> argument. A value can also be converted to <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> with the <span class="function"><a href="function.intval.html" class="function">intval()</a></span> function. </p> <p class="simpara"> See also <a href="language.types.type-juggling.html" class="link">Type Juggling</a>. </p> <div class="sect3" id="language.types.integer.casting.from-boolean"> <h4 class="title">From <a href="language.types.boolean.html" class="link">booleans</a></h4> <p class="simpara"> <strong><code>FALSE</code></strong> will yield <em>0</em> (zero), and <strong><code>TRUE</code></strong> will yield <em>1</em> (one). </p> </div> <div class="sect3" id="language.types.integer.casting.from-float"> <h4 class="title"> From <a href="language.types.float.html" class="link">floating point numbers</a> </h4> <p class="simpara"> When converting from <span class="type"><a href="language.types.float.html" class="type float">float</a></span> to <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>, the number will be rounded <em class="emphasis">towards zero</em>. </p> <p class="para"> If the float is beyond the boundaries of <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> (usually <em>+/- 2.15e+9 = 2^31</em> on 32-bit platforms and <em>+/- 9.22e+18 = 2^63</em> on 64-bit platforms), the result is undefined, since the <span class="type"><a href="language.types.float.html" class="type float">float</a></span> doesn't have enough precision to give an exact <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> result. No warning, not even a notice will be issued when this happens! </p> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> Never cast an unknown fraction to <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>, as this can sometimes lead to unexpected results. </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo (int) ( (</span><span style="color: #0000BB">0.1</span><span style="color: #007700">+</span><span style="color: #0000BB">0.7</span><span style="color: #007700">) * </span><span style="color: #0000BB">10 </span><span style="color: #007700">); </span><span style="color: #FF8000">// echoes 7!<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> See also the <a href="language.types.float.html#warn.float-precision" class="link">warning about float precision</a>. </p> </div> </div> <div class="sect3" id="language.types.integer.casting.from-string"> <h4 class="title">From strings</h4> <p class="simpara"> See <a href="language.types.string.html#language.types.string.conversion" class="link">String conversion to numbers</a> </p> </div> <div class="sect3" id="language.types.integer.casting.from-other"> <h4 class="title">From other types</h4> <div class="caution"><strong class="caution">Caution</strong> <p class="simpara"> The behaviour of converting to <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> is undefined for other types. Do <em class="emphasis">not</em> rely on any observed behaviour, as it can change without notice. </p> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.boolean.html">Booleans</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.float.html">Floating point numbers</a></div> <div class="up"><a href="language.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>