-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathnumeric-strings.xml
143 lines (134 loc) · 5.29 KB
/
numeric-strings.xml
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
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 7ba2406e555a240338d63f72d9ac54a46e0bee5d Maintainer: Annlix Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<sect1 xml:id="language.types.numeric-strings" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>数字字符串</title>
<para>
如果一个 PHP <type>string</type> 可以被解释为 <type>int</type> 或 <type>float</type> 类型,则它被视为数字字符串。
</para>
<para>
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 也有<emphasis>前导</emphasis>数字字符串的概念。
这只是一个字符串,其开头类似于数字字符串,后跟任何字符。
</para>
<note>
<para>
任何包含字母 <literal>E</literal>
周围是数字的字符串都将视为以科学计数法表示的数字。这会产生意想不到的效果。
<programlisting role="php">
<![CDATA[
<?php
var_dump("0D1" == "000"); // false, "0D1" 不是科学计数法
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>在数字上下文中使用的字符串</title>
<para>
当一个 <type>string</type> 需要被当作一个数字计算时,(例如:算术运算, <type>int</type>
类型声明等),则采取以下步骤来确定结果:
<orderedlist>
<listitem>
<simpara>
如果 <type>string</type> 是数字,当 <type>string</type>
是整数字符串并且符合 <type>int</type> 类型的范围限制(即是 PHP_INT_MAX
定义的值),则解析为 <type>int</type> ,否则解析为 <type>float</type> 。
</simpara>
</listitem>
<listitem>
<simpara>
如果上下文允许前导数字和一个 <type>string</type>,如果 <type>string</type> 的前导部分是整数数字字符串且符合 <type>int</type>
类型限制(由 <constant>PHP_INT_MAX</constant> 定义),则解析为 <type>int</type> ,否则解析为 <type>float</type> 。
此外,还会导致 <constant>E_WARNING</constant> 级别的错误。
</simpara>
</listitem>
<listitem>
<simpara>
如果 <type>string</type> 不是数字,则会抛出一个 <classname>TypeError</classname> 的异常。
</simpara>
</listitem>
</orderedlist>
</para>
</sect2>
<sect2 xml:id="language.types.numeric-string.prior">
<title> PHP 8.0.0 之前的行为</title>
<para>
在 PHP 8.0.0 之前,
只有在<emphasis>前导</emphasis>空格的时候,<type>string</type> 才被认为是数字;如果它有<emphasis>尾随</emphasis>空格,则该字符串被视为是前导数字。
</para>
<para>
在 PHP 8.0.0 之前,当在数字上下文中使用字符串时,它将执行与上述相同的步骤,但有以下区别:
<itemizedlist>
<listitem>
<simpara>
使用前导数字字符串将导致 <constant>E_NOTICE</constant>
而不是 <constant>E_WARNING</constant> 错误。
</simpara>
</listitem>
<listitem>
<simpara>
如果字符串不是数字,则会导致 <constant>E_WARNING</constant>
错误并返回 <literal>0</literal> 。
</simpara>
</listitem>
</itemizedlist>
在 PHP 7.1.0 之前,则既不会导致 <constant>E_NOTICE</constant>,也不会导致 <constant>E_WARNING</constant>。
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$foo = 1 + "10.5"; // $foo 是 float (11.5)
$foo = 1 + "-1.3e3"; // $foo 是 float (-1299)
$foo = 1 + "bob-1.3e3"; // PHP 8.0.0 起产生 TypeError;在此之前 $foo 是 integer (1)
$foo = 1 + "bob3"; // PHP 8.0.0 起产生 TypeError;在此之前 $foo 是 integer (1)
$foo = 1 + "10 Small Pigs"; // PHP 8.0.0 起,$foo 是 integer (11),并且产生 E_WARNING;在此之前产生 E_NOTICE
$foo = 4 + "10.2 Little Piggies"; // PHP 8.0.0 起,$foo 是 float (14.2),并且产生 E_WARNING;在此之前产生 E_NOTICE
$foo = "10.0 pigs " + 1; // PHP 8.0.0 起,$foo 是 float (11),并且产生 E_WARNING;在此之前产生 E_NOTICE
$foo = "10.0 pigs " + 1.0; // PHP 8.0.0 起,$foo 是 float (11),并且产生 E_WARNING;在此之前产生 E_NOTICE
?>
]]>
</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
-->