mirror of
https://github.com/php/doc-en.git
synced 2026-03-23 23:32:18 +01:00
160 lines
5.4 KiB
XML
160 lines
5.4 KiB
XML
<?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.
|
|
</para>
|
|
<example>
|
|
<title>Scientific Notation Comparisons</title>
|
|
<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>
|
|
</example>
|
|
</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
|
|
-->
|