<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.types.integer.php',
    1 => 'Integers',
    2 => 'Integers',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Types',
  ),
  'prev' => 
  array (
    0 => 'language.types.boolean.php',
    1 => 'Booleans',
  ),
  'next' => 
  array (
    0 => 'language.types.float.php',
    1 => 'Floating point numbers',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/types/integer.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.integer" class="sect1">
 <h2 class="title">Integers</h2>

 <p class="simpara">
  An <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> is a number of the set
  ℤ = {..., -2, -1, 0, 1, 2, ...}.
 </p>

 <div class="sect2">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="language.types.float.php" class="link">Floating point numbers</a></li>
    <li><a href="book.bc.php" class="link">Arbitrary precision / BCMath</a></li>
    <li><a href="book.gmp.php" class="link">Arbitrary length integer / GMP</a></li>
   </ul>
  </p>
 </div>

 <div class="sect2" id="language.types.integer.syntax">
  <h3 class="title">Syntax</h3>

  <p class="simpara">
   <span class="type"><a href="language.types.integer.php" class="type Int">Int</a></span>s can be specified in decimal (base 10), hexadecimal
   (base 16), octal (base 8) or binary (base 2) notation.
   The <a href="language.operators.arithmetic.php" class="link">negation operator</a>
   can be used to denote a negative <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
  </p>

  <p class="para">
   To use octal notation, precede the number with a <code class="literal">0</code> (zero).
   As of PHP 8.1.0, octal notation can also be preceded with <code class="literal">0o</code> or <code class="literal">0O</code>.
   To use hexadecimal notation precede the number with <code class="literal">0x</code>.
   To use binary notation precede the number with <code class="literal">0b</code>.
  </p>

  <p class="para">
   As of PHP 7.4.0, integer literals may contain underscores (<code class="literal">_</code>) between digits,
   for better readability of literals. These underscores are removed by PHP&#039;s scanner.
  </p>

  <div class="example" id="example-1">
   <p><strong>Example #1 Integer literals</strong></p>
   <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">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">0o123</span><span style="color: #007700">; </span><span style="color: #FF8000">// octal number (as of PHP 8.1.0)<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">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1_234_567</span><span style="color: #007700">; </span><span style="color: #FF8000">// decimal number (as of PHP 7.4.0)<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="para">
   Formally, the structure for <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> literals is as of PHP 8.1.0
   (previously, the <code class="literal">0o</code> or <code class="literal">0O</code> octal
   prefixes were not allowed, and prior to PHP 7.4.0 the underscores were
   not allowed):
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive cdata"><pre>
decimal     : [1-9][0-9]*(_[0-9]+)*
            | 0

hexadecimal : 0[xX][0-9a-fA-F]+(_[0-9a-fA-F]+)*

octal       : 0[oO]?[0-7]+(_[0-7]+)*

binary      : 0[bB][01]+(_[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.php" class="type int">int</a></span> is platform-dependent, although a maximum
   value of about two billion is the usual value (that&#039;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.php" class="type int">int</a></span>s.
   <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> size can be determined
   using the constant <strong><code><a href="reserved.constants.php#constant.php-int-size">PHP_INT_SIZE</a></code></strong>, maximum value using
   the constant <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>,
   and minimum value using the constant <strong><code><a href="reserved.constants.php#constant.php-int-min">PHP_INT_MIN</a></code></strong>.
  </p>
 </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.php" class="type int">int</a></span>
   type, it will be interpreted as a <span class="type"><a href="language.types.float.php" 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.php" class="type int">int</a></span> type will return a <span class="type"><a href="language.types.float.php" class="type float">float</a></span> instead.
  </p>

  <div class="example" id="example-2">
   <p><strong>Example #2 Integer overflow</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$large_number </span><span style="color: #007700">= </span><span style="color: #0000BB">50000000000000000000</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 /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">PHP_INT_MAX </span><span style="color: #007700">+ </span><span style="color: #0000BB">1</span><span style="color: #007700">);       </span><span style="color: #FF8000">// 32-bit system: float(2147483648)<br />                                 // 64-bit system: float(9.2233720368548E+18)<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>

 <div class="sect2" id="language.types.integer.division">
  <h3 class="title">Integer division</h3>

  <p class="para">
   There is no <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> division operator in PHP, to achieve this
   use the <span class="function"><a href="function.intdiv.php" class="function">intdiv()</a></span> function.
   <code class="literal">1/2</code> yields the <span class="type"><a href="language.types.float.php" class="type float">float</a></span> <code class="literal">0.5</code>.
   The value can be cast to an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> to round it towards zero, or
   the <span class="function"><a href="function.round.php" class="function">round()</a></span> function provides finer control over rounding.
  </p>

  <div class="example" id="example-3">
   <p><strong>Example #3 Divisions</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</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.php" class="type int">int</a></span>, use the <code class="literal">(int)</code> cast.
   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.php" class="type int">int</a></span> argument. A value can also be converted to
   <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> with the <span class="function"><a href="function.intval.php" class="function">intval()</a></span> function.
  </p>

  <p class="simpara">
   If a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> is converted to an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>, then
   the result will be the unique resource number assigned to the
   <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> by PHP at runtime.
  </p>

  <p class="simpara">
   See also <a href="language.types.type-juggling.php" 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.php" class="link">booleans</a></h4>

   <p class="simpara">
    <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> will yield <code class="literal">0</code> (zero), and <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> will yield
    <code class="literal">1</code> (one).
   </p>
  </div>

  <div class="sect3" id="language.types.integer.casting.from-float">
   <h4 class="title">
    From <a href="language.types.float.php" class="link">floating point numbers</a>
   </h4>

   <p class="simpara">
    When converting from <span class="type"><a href="language.types.float.php" class="type float">float</a></span> to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>, the number
    will be rounded <em>towards zero</em>.
    As of PHP 8.1.0, a deprecation notice is emitted when implicitly converting a non-integral <span class="type"><a href="language.types.float.php" class="type float">float</a></span> to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> which loses precision.
   </p>

   <div class="example" id="example-4">
    <p><strong>Example #4 Casting from Float</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">): </span><span style="color: #0000BB">int </span><span style="color: #007700">{<br />  return </span><span style="color: #0000BB">$value</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">8.1</span><span style="color: #007700">)); </span><span style="color: #FF8000">// "Deprecated: Implicit conversion from float 8.1 to int loses precision" as of PHP 8.1.0<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">8.1</span><span style="color: #007700">)); </span><span style="color: #FF8000">// 8 prior to PHP 8.1.0<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">foo</span><span style="color: #007700">(</span><span style="color: #0000BB">8.0</span><span style="color: #007700">)); </span><span style="color: #FF8000">// 8 in both cases<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((int) </span><span style="color: #0000BB">8.1</span><span style="color: #007700">); </span><span style="color: #FF8000">// 8 in both cases<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">intval</span><span style="color: #007700">(</span><span style="color: #0000BB">8.1</span><span style="color: #007700">)); </span><span style="color: #FF8000">// 8 in both cases<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>

   <p class="para">
    If the float is beyond the boundaries of <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> (usually
    <code class="literal">+/- 2.15e+9 = 2^31</code> on 32-bit platforms and
    <code class="literal">+/- 9.22e+18 = 2^63</code> on 64-bit platforms),
    the result is undefined, since the <span class="type"><a href="language.types.float.php" class="type float">float</a></span> doesn&#039;t
    have enough precision to give an exact <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> result.
    No warning, not even a notice will be issued when this happens!
   </p>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     <code class="literal">NaN</code>, <code class="literal">Inf</code> and <code class="literal">-Inf</code> will always be zero when cast to <span class="type"><a href="language.types.integer.php" class="type int">int</a></span>.
    </p>
   </p></blockquote>

   <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.php" class="type int">int</a></span>, as this can
     sometimes lead to unexpected results.
    </p>

    <div class="informalexample">
     <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;</span></span></code></div>
     </div>

    </div>

    <p class="para">
     See also the <a href="language.types.float.php#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">
    If the string is
    <a href="language.types.numeric-strings.php" class="link">numeric</a>
    or leading numeric then it will resolve to the
    corresponding integer value, otherwise it is converted to zero
    (<code class="literal">0</code>).
   </p>
  </div>

  <div class="sect3" id="language.types.integer.casting-from-null">
   <h4 class="title">From <span class="type"><a href="language.types.null.php" class="type NULL">NULL</a></span></h4>

   <p class="simpara">
    <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> is always converted to zero (<code class="literal">0</code>).
   </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.php" class="type int">int</a></span> is undefined for other
     types. Do <em>not</em> rely on any observed behaviour, as it
     can change without notice.
    </p>
   </div>
  </div>

 </div>
</div><?php manual_footer($setup); ?>