1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<sect1 xml:id="language.types.numeric-strings" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Numeric strings</title>
<para>
A PHP <type>string</type> is considered numeric if it can be interpreted as
an <type>int</type> or a <type>float</type>.
</para>
<para>
Formally as of PHP 8.0.0:
</para>
<informalexample>
<programlisting>
<![CDATA[
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})
]]>
</programlisting>
</informalexample>
<para>
PHP also has a concept of <emphasis>leading</emphasis> numeric strings.
This is simply a string which starts like a numeric string followed by
any characters.
</para>
<note>
<para>
Any string that contains the letter <literal>E</literal> (case insensitive)
bounded by numbers will be seen as a number expressed in scientific notation.
This can produce unexpected results.
<programlisting role="php">
<![CDATA[
<?php
var_dump("0D1" == "000"); // false, "0D1" is not scientific notation
var_dump("0E1" == "000"); // true, "0E1" is 0 * (10 ^ 1), or 0
var_dump("2E1" == "020"); // true, "2E1" is 2 * (10 ^ 1), or 20
?>
]]>
</programlisting>
</para>
</note>
<sect2 xml:id="language.types.numeric-string.conversion">
<title>Strings used in numeric contexts</title>
<para>
When a <type>string</type> needs to be evaluated as number (e.g. arithmetic
operations, <type>int</type> type declaration, etc.) the following
steps are taken to determine the outcome:
<orderedlist>
<listitem>
<simpara>
If the <type>string</type> is numeric, resolve to an <type>int</type> if
the <type>string</type> is an integer numeric string and fits into the
limits of the <type>int</type> type limits (as defined by
<constant>PHP_INT_MAX</constant>), otherwise resolve to a
<type>float</type>.
</simpara>
</listitem>
<listitem>
<simpara>
If the context allows leading numeric strings and the <type>string</type>
is one, resolve to an <type>int</type> if the leading part of the
<type>string</type> is an integer numeric string and fits into the
limits of the <type>int</type> type limits (as defined by
<constant>PHP_INT_MAX</constant>), otherwise resolve to a
<type>float</type>.
Additionally an error of level <constant>E_WARNING</constant> is raised.
</simpara>
</listitem>
<listitem>
<simpara>
The <type>string</type> is not numeric, throw a
<classname>TypeError</classname>.
</simpara>
</listitem>
</orderedlist>
</para>
</sect2>
<sect2 xml:id="language.types.numeric-string.prior">
<title>Behavior prior to PHP 8.0.0</title>
<para>
Prior to PHP 8.0.0, a <type>string</type> was considered numeric only if it
had <emphasis>leading</emphasis> whitespaces, if it had
<emphasis>trailing</emphasis> whitespaces then the string was considered to
be leading numeric.
</para>
<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:
<itemizedlist>
<listitem>
<simpara>
The usage of a leading numeric string would raise an
<constant>E_NOTICE</constant> instead of an <constant>E_WARNING</constant>.
</simpara>
</listitem>
<listitem>
<simpara>
If the string is not numeric, an <constant>E_WARNING</constant> was
raised and the value <literal>0</literal> would be returned.
</simpara>
</listitem>
</itemizedlist>
Prior to PHP 7.1.0, neither <constant>E_NOTICE</constant>
nor <constant>E_WARNING</constant> was raised.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "-1.3e3"; // $foo is float (-1299)
$foo = 1 + "bob-1.3e3"; // TypeError as of PHP 8.0.0, $foo is integer (1) previously
$foo = 1 + "bob3"; // TypeError as of PHP 8.0.0, $foo is integer (1) previously
$foo = 1 + "10 Small Pigs"; // $foo is integer (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
$foo = "10.0 pigs " + 1; // $foo is float (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
$foo = "10.0 pigs " + 1.0; // $foo is float (11) and an E_WARNING is raised in PHP 8.0.0, E_NOTICE previously
?>
]]>
</programlisting>
</informalexample>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|