Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 9459

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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="book.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="book.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.
   The <a href="language.operators.arithmetic.html" class="link">negation operator</a>
   can be used to denote a negative <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>.
  </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-41">
   <p><strong>Example #1 Integer literals</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1234</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;decimal&nbsp;number<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;-</span><span style="color: #0000BB">123</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;a&nbsp;negative&nbsp;number<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0123</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;octal&nbsp;number&nbsp;(equivalent&nbsp;to&nbsp;83&nbsp;decimal)<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0x1A</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;hexadecimal&nbsp;number&nbsp;(equivalent&nbsp;to&nbsp;26&nbsp;decimal)<br /></span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0b11111111</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//&nbsp;binary&nbsp;number&nbsp;(equivalent&nbsp;to&nbsp;255&nbsp;decimal)<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.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      : 0[bB][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&#039;s 32 bits signed).
   64-bit platforms usually have a maximum value of about 9E18, except on
   Windows prior to PHP 7, where it was always 32 bit. 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>, maximum value using
   the constant <strong><code>PHP_INT_MAX</code></strong> since PHP 5.0.5,
   and minimum value using the constant <strong><code>PHP_INT_MIN</code></strong> since
   PHP 7.0.0.
  </p>

  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    Prior to PHP 7, if an invalid digit was 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 was ignored. Since PHP 7, a parse error
    is emitted.
   </p>
  </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-42">
   <p><strong>Example #2 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">&lt;?php<br />$large_number&nbsp;</span><span style="color: #007700">=&nbsp;</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">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;int(2147483647)<br /><br /></span><span style="color: #0000BB">$large_number&nbsp;</span><span style="color: #007700">=&nbsp;</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">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float(2147483648)<br /><br /></span><span style="color: #0000BB">$million&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1000000</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$large_number&nbsp;</span><span style="color: #007700">=&nbsp;&nbsp;</span><span style="color: #0000BB">50000&nbsp;</span><span style="color: #007700">*&nbsp;</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">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float(50000000000)<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>

  <div class="example" id="example-43">
   <p><strong>Example #3 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">&lt;?php<br />$large_number&nbsp;</span><span style="color: #007700">=&nbsp;</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">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;int(9223372036854775807)<br /><br /></span><span style="color: #0000BB">$large_number&nbsp;</span><span style="color: #007700">=&nbsp;</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">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float(9.2233720368548E+18)<br /><br /></span><span style="color: #0000BB">$million&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1000000</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$large_number&nbsp;</span><span style="color: #007700">=&nbsp;&nbsp;</span><span style="color: #0000BB">50000000000000&nbsp;</span><span style="color: #007700">*&nbsp;</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">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float(5.0E+19)<br /></span><span style="color: #0000BB">?&gt;</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 towards zero, 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">&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">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;float(3.5714285714286)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">((int)&nbsp;(</span><span style="color: #0000BB">25</span><span style="color: #007700">/</span><span style="color: #0000BB">7</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;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">));&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;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.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">
   If a <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> is converted to an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>, then
   the result will be the unique resource number assigned to the
   <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span> by PHP at runtime.
  </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 other than
    Windows), the result is undefined, since the <span class="type"><a href="language.types.float.html" class="type float">float</a></span> doesn&#039;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>

   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     As of PHP 7.0.0, instead of being undefined and platform-dependent, NaN and Infinity will
     always be zero when cast to <span class="type"><a href="language.types.integer.html" class="type integer">integer</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.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">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;(int)&nbsp;(&nbsp;(</span><span style="color: #0000BB">0.1</span><span style="color: #007700">+</span><span style="color: #0000BB">0.7</span><span style="color: #007700">)&nbsp;*&nbsp;</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;echoes&nbsp;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.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-null">
   <h4 class="title">From <span class="type"><a href="language.types.null.html" class="type NULL">NULL</a></span></h4>

   <p class="simpara">
    <strong><code>NULL</code></strong> is always converted to zero (<em>0</em>).
   </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>