-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathnumeric-strings.xml
More file actions
157 lines (149 loc) · 5.77 KB
/
numeric-strings.xml
File metadata and controls
157 lines (149 loc) · 5.77 KB
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
157
<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: e587d0655e426f97b3fcb431453da5030e743b23 Maintainer: PhilDaiguille Status: ready -->
<!-- Reviewed: yes Maintainer: Marqitos -->
<sect1 xml:id="language.types.numeric-strings" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Strings numéricos</title>
<para>
Un <type>string</type> PHP se considera numérico si puede ser interpretado como
un <type>int</type> o un <type>float</type>.
</para>
<para>
Formalmente a partir de 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 también dispone de un concepto de strings <emphasis>iniciando</emphasis> numéricamente.
Se trata simplemente de un string que comienza como un string numérico seguido de
cualquier carácter.
</para>
<note>
<para>
Cualquier string que contenga la letra <literal>E</literal> (insensible a mayúsculas y minúsculas)
delimitada por números será considerada como un número expresado en notación científica.
Esto puede producir resultados inesperados.
</para>
<example>
<title>Scientific Notation Comparisons</title>
<programlisting role="php">
<![CDATA[
<?php
var_dump("0D1" == "000"); // false, "0D1" no es notación científica
var_dump("0E1" == "000"); // true, "0E1" es 0 * (10 ^ 1), o 0
var_dump("2E1" == "020"); // true, "2E1" es 2 * (10 ^ 1), o 20
?>
]]>
</programlisting>
</example>
</note>
<sect2 xml:id="language.types.numeric-string.conversion">
<title>Strings utilizados en contextos numéricos</title>
<para>
Cuando una <type>string</type> debe ser evaluada como un número (por ejemplo, operaciones
aritméticas, declaración de tipo <type>int</type>, etc.),
se siguen los siguientes pasos para determinar el resultado:
<orderedlist>
<listitem>
<simpara>
Si el <type>string</type> es numérico, se resuelve en <type>int</type> si
el <type>string</type> es un string numérico entero y entra dentro de los
límites del tipo <type>int</type> (tales como definidas por
<constant>PHP_INT_MAX</constant>), de lo contrario se resuelve en un
<type>float</type>.
</simpara>
</listitem>
<listitem>
<simpara>
Si el contexto permite los strings iniciando numéricamente y el <type>string</type>
es uno de ellos, se resuelve en <type>int</type> si la parte inicial del
<type>string</type> es un string numérico entero y entra dentro de los
límites del tipo <type>int</type> (tales como definidas por
<constant>PHP_INT_MAX</constant>), de lo contrario se resuelve en un
<type>float</type>.
Además, se genera un error de nivel <constant>E_WARNING</constant>.
</simpara>
</listitem>
<listitem>
<simpara>
Si la <type>string</type> no es numérica, se lanza una <classname>TypeError</classname>.
</simpara>
</listitem>
</orderedlist>
</para>
</sect2>
<sect2 xml:id="language.types.numeric-string.prior">
<title>Comportamiento anterior a PHP 8.0.0</title>
<para>
Antes de PHP 8.0.0, un <type>string</type> se consideraba numérico solo si tenía espacios en blanco al
<emphasis>inicio</emphasis> del string, si tenía espacios en blanco al
<emphasis>final</emphasis> del string, el string se consideraba como un string iniciando numéricamente.
</para>
<para>
Antes de PHP 8.0.0, cuando un string se utilizaba en un contexto numérico,
seguía los mismos pasos que los anteriores, con las siguientes diferencias:
<itemizedlist>
<listitem>
<simpara>
El uso de un string iniciando numéricamente generaba un error de tipo
<constant>E_NOTICE</constant> en lugar de <constant>E_WARNING</constant>.
</simpara>
</listitem>
<listitem>
<simpara>
Si el string no era numérico, se generaba un <constant>E_WARNING</constant>
y se devolvía el valor <literal>0</literal>.
</simpara>
</listitem>
</itemizedlist>
Anterior a PHP 7.1.0, ni <constant>E_NOTICE</constant>
ni <constant>E_WARNING</constant> se generaban.
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = 1 + "10.5"; // $foo es float (11.5)
$foo = 1 + "-1.3e3"; // $foo es float (-1299)
$foo = 1 + "bob-1.3e3"; // TypeError a partir de PHP 8.0.0, $foo era integer (1) anteriormente
$foo = 1 + "bob3"; // TypeError a partir de PHP 8.0.0, $foo era integer (1) anteriormente
$foo = 1 + "10 Small Pigs"; // $foo es integer (11) y se genera un E_WARNING en PHP 8.0.0, E_NOTICE anteriormente
$foo = 4 + "10.2 Little Piggies"; // $foo es float (14.2) y se genera un E_WARNING en PHP 8.0.0, E_NOTICE anteriormente
$foo = "10.0 pigs " + 1; // $foo es float (11) y se genera un E_WARNING en PHP 8.0.0, E_NOTICE anteriormente
$foo = "10.0 pigs " + 1.0; // $foo es float (11) y se genera un E_WARNING en PHP 8.0.0, E_NOTICE anteriormente
?>
]]>
</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
-->