<?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.numeric-strings.php',
    1 => 'Numeric strings',
    2 => 'Numeric strings',
  ),
  'up' => 
  array (
    0 => 'language.types.php',
    1 => 'Types',
  ),
  'prev' => 
  array (
    0 => 'language.types.string.php',
    1 => 'Strings',
  ),
  'next' => 
  array (
    0 => 'language.types.array.php',
    1 => 'Arrays',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/types/numeric-strings.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.types.numeric-strings" class="sect1">
 <h2 class="title">Numeric strings</h2>
 <p class="para">
  A PHP <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is considered numeric if it can be interpreted as
  an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> or a <span class="type"><a href="language.types.float.php" class="type float">float</a></span>.
 </p>

 <p class="para">
  Formally as of PHP 8.0.0:
 </p>

 <div class="informalexample">
  <div class="example-contents">
   <div class="annotation-interactive cdata"><pre>
WHITESPACES      \s*
LNUM             [0-9]+
DNUM             ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
EXPONENT_DNUM    (({LNUM} | {DNUM}) [eE][+-]? {LNUM})
INT_NUM_STRING   {WHITESPACES} [+-]? {LNUM} {WHITESPACES}
FLOAT_NUM_STRING {WHITESPACES} [+-]? ({DNUM} | {EXPONENT_DNUM}) {WHITESPACES}
NUM_STRING       ({INT_NUM_STRING} | {FLOAT_NUM_STRING})
</pre></div>
  </div>

 </div>

 <p class="para">
  PHP also has a concept of <em>leading</em> numeric strings.
  This is simply a string which starts like a numeric string followed by
  any characters.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   Any string that contains the letter <code class="literal">E</code> (case insensitive)
   bounded by numbers will be seen as a number expressed in scientific notation.
   This can produce unexpected results.
  </p>
  <div class="example" id="example-1">
   <p><strong>Example #1 Scientific Notation Comparisons</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: #DD0000">"0D1" </span><span style="color: #007700">== </span><span style="color: #DD0000">"000"</span><span style="color: #007700">); </span><span style="color: #FF8000">// false, "0D1" is not scientific notation<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"0E1" </span><span style="color: #007700">== </span><span style="color: #DD0000">"000"</span><span style="color: #007700">); </span><span style="color: #FF8000">// true, "0E1" is 0 * (10 ^ 1), or 0<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #DD0000">"2E1" </span><span style="color: #007700">== </span><span style="color: #DD0000">"020"</span><span style="color: #007700">); </span><span style="color: #FF8000">// true, "2E1" is 2 * (10 ^ 1), or 20<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </p></blockquote>

 <div class="sect2" id="language.types.numeric-string.conversion">
  <h3 class="title">Strings used in numeric contexts</h3>
  <p class="para">
   When a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> needs to be evaluated as number (e.g. arithmetic
   operations, <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type declaration, etc.) the following
   steps are taken to determine the outcome:

   <ol type="1">
    <li class="listitem">
     <span class="simpara">
      If the <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is numeric, resolve to an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> if
      the <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is an integer numeric string and fits into the
      limits of the <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type limits (as defined by
      <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>), otherwise resolve to a
      <span class="type"><a href="language.types.float.php" class="type float">float</a></span>.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      If the context allows leading numeric strings and the <span class="type"><a href="language.types.string.php" class="type string">string</a></span>
      is one, resolve to an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> if the leading part of the
      <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is an integer numeric string and fits into the
      limits of the <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> type limits (as defined by
      <strong><code><a href="reserved.constants.php#constant.php-int-max">PHP_INT_MAX</a></code></strong>), otherwise resolve to a
      <span class="type"><a href="language.types.float.php" class="type float">float</a></span>.
      Additionally an error of level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> is raised.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      The <span class="type"><a href="language.types.string.php" class="type string">string</a></span> is not numeric, throw a
      <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span>.
     </span>
    </li>
   </ol>
  </p>
 </div>

 <div class="sect2" id="language.types.numeric-string.prior">
  <h3 class="title">Behavior prior to PHP 8.0.0</h3>
  <p class="para">
   Prior to PHP 8.0.0, a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> was considered numeric only if it
   had <em>leading</em> whitespaces, if it had
   <em>trailing</em> whitespaces then the string was considered to
   be leading numeric.
  </p>

  <p class="para">
   Prior to PHP 8.0.0, when a string was used in a numeric context it would
   perform the same steps as above with the following differences:
   <ul class="itemizedlist">
    <li class="listitem">
     <span class="simpara">
      The usage of a leading numeric string would raise an
      <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong> instead of an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>.
     </span>
     </li>
    <li class="listitem">
     <span class="simpara">
      If the string is not numeric, an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was
      raised and the value <code class="literal">0</code> would be returned.
     </span>
    </li>
   </ul>
   Prior to PHP 7.1.0, neither <strong><code><a href="errorfunc.constants.php#constant.e-notice">E_NOTICE</a></code></strong>
   nor <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> was raised.
  </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 />$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"10.5"</span><span style="color: #007700">;                </span><span style="color: #FF8000">// $foo is float (11.5)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"-1.3e3"</span><span style="color: #007700">;              </span><span style="color: #FF8000">// $foo is float (-1299)<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"bob-1.3e3"</span><span style="color: #007700">;           </span><span style="color: #FF8000">// TypeError as of PHP 8.0.0, $foo is integer (1) previously<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"bob3"</span><span style="color: #007700">;                </span><span style="color: #FF8000">// TypeError as of PHP 8.0.0, $foo is integer (1) previously<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">1 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"10 Small Pigs"</span><span style="color: #007700">;       </span><span style="color: #FF8000">// $foo is integer (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #0000BB">4 </span><span style="color: #007700">+ </span><span style="color: #DD0000">"10.2 Little Piggies"</span><span style="color: #007700">; </span><span style="color: #FF8000">// $foo is float (14.2) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"10.0 pigs " </span><span style="color: #007700">+ </span><span style="color: #0000BB">1</span><span style="color: #007700">;          </span><span style="color: #FF8000">// $foo is float (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously<br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"10.0 pigs " </span><span style="color: #007700">+ </span><span style="color: #0000BB">1.0</span><span style="color: #007700">;        </span><span style="color: #FF8000">// $foo is float (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

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