reference/var/functions/is-numeric.xml
0817d5b2835f8a47314823338d983fa2c5005dfc
...
...
@@ -12,14 +12,11 @@
12
12
&reftitle.description;
13
13
<methodsynopsis>
14
14
<type>bool</type><methodname>is_numeric</methodname>
15
-
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
15
+
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
16
16
</methodsynopsis>
17
17
<para>
18
-
Finds whether the given variable is numeric. Numeric strings consist of optional
19
-
sign, any number of digits, optional decimal part and optional exponential part.
20
-
Thus <literal>+0123.45e6</literal> is a valid numeric value. Hexadecimal (e.g.
21
-
<literal>0xf4c3b00c</literal>) and binary (e.g. <literal>0b10100111001</literal>)
22
-
notation is not allowed.
18
+
Determines if the given variable is a number or a
19
+
<link linkend="language.types.numeric-strings">numeric string</link>.
23
20
</para>
24
21
</refsect1>
25
22

...
...
@@ -28,7 +25,7 @@
28
25
<para>
29
26
<variablelist>
30
27
<varlistentry>
31
-
<term><parameter>var</parameter></term>
28
+
<term><parameter>value</parameter></term>
32
29
<listitem>
33
30
<para>
34
31
The variable being evaluated.
...
...
@@ -42,11 +39,35 @@
42
39
<refsect1 role="returnvalues">
43
40
&reftitle.returnvalues;
44
41
<para>
45
-
Returns &true; if <parameter>var</parameter> is a number or a numeric
46
-
string, &false; otherwise.
42
+
Returns &true; if <parameter>value</parameter> is a number or a
43
+
<link linkend="language.types.numeric-strings">numeric string</link>,
44
+
&false; otherwise.
47
45
</para>
48
46
</refsect1>
49
47

48
+
<refsect1 role="changelog">
49
+
&reftitle.changelog;
50
+
<informaltable>
51
+
<tgroup cols="2">
52
+
<thead>
53
+
<row>
54
+
<entry>&Version;</entry>
55
+
<entry>&Description;</entry>
56
+
</row>
57
+
</thead>
58
+
<tbody>
59
+
<row>
60
+
<entry>8.0.0</entry>
61
+
<entry>
62
+
Numeric strings ending with whitespace (<literal>"42 "</literal>) will now
63
+
return &true;. Previously, &false; was returned instead.
64
+
</entry>
65
+
</row>
66
+
</tbody>
67
+
</tgroup>
68
+
</informaltable>
69
+
</refsect1>
70
+

50
71
<refsect1 role="examples">
51
72
&reftitle.examples;
52
73
<para>
...
...
@@ -62,10 +83,15 @@ $tests = array(
62
83
02471,
63
84
0b10100111001,
64
85
1337e0,
86
+
"0x539",
87
+
"02471",
88
+
"0b10100111001",
89
+
"1337e0",
65
90
"not numeric",
66
91
array(),
67
92
9.1,
68
-
null
93
+
null,
94
+
'',
69
95
);
70
96

71
97
foreach ($tests as $element) {
...
...
@@ -86,47 +112,72 @@ foreach ($tests as $element) {
86
112
1337 is numeric
87
113
1337 is numeric
88
114
1337 is numeric
89
-
1337 is numeric
115
+
1337.0 is numeric
116
+
'0x539' is NOT numeric
117
+
'02471' is numeric
118
+
'0b10100111001' is NOT numeric
119
+
'1337e0' is numeric
90
120
'not numeric' is NOT numeric
91
-
array () is NOT numeric
92
-
9.0999999999999996447286321199499070644378662109375 is numeric
121
+
array (
122
+
) is NOT numeric
123
+
9.1 is numeric
93
124
NULL is NOT numeric
125
+
'' is NOT numeric
94
126
]]>
95
127
</screen>
96
128
</example>
97
129
</para>
98
-
</refsect1>
99
130

100
-
<refsect1 role="changelog"><!-- {{{ -->
101
-
&reftitle.changelog;
102
131
<para>
103
-
<informaltable>
104
-
<tgroup cols="2">
105
-
<thead>
106
-
<row>
107
-
<entry>&Version;</entry>
108
-
<entry>&Description;</entry>
109
-
</row>
110
-
</thead>
111
-
<tbody>
112
-
<row>
113
-
<entry>7.0.0</entry>
114
-
<entry>
115
-
Strings in hexadecimal (e.g. <literal>0xf4c3b00c</literal>) notation are
116
-
no longer regarded as numeric strings, i.e.
117
-
<function>is_numeric</function> returns &false; now.
118
-
</entry>
119
-
</row>
120
-
</tbody>
121
-
</tgroup>
122
-
</informaltable>
132
+
<example>
133
+
<title><function>is_numeric</function> with whitespace</title>
134
+
<programlisting role="php">
135
+
<![CDATA[
136
+
<?php
137
+
$tests = [
138
+
" 42",
139
+
"42 ",
140
+
"\u{A0}9001", // non-breaking space
141
+
"9001\u{A0}", // non-breaking space
142
+
];
143
+

144
+
foreach ($tests as $element) {
145
+
if (is_numeric($element)) {
146
+
echo var_export($element, true) . " is numeric", PHP_EOL;
147
+
} else {
148
+
echo var_export($element, true) . " is NOT numeric", PHP_EOL;
149
+
}
150
+
}
151
+
?>
152
+
]]>
153
+
</programlisting>
154
+
&example.outputs.8;
155
+
<screen>
156
+
<![CDATA[
157
+
' 42' is numeric
158
+
'42 ' is numeric
159
+
' 9001' is NOT numeric
160
+
'9001 ' is NOT numeric
161
+
]]>
162
+
</screen>
163
+
&example.outputs.7;
164
+
<screen>
165
+
<![CDATA[
166
+
' 42' is numeric
167
+
'42 ' is NOT numeric
168
+
' 9001' is NOT numeric
169
+
'9001 ' is NOT numeric
170
+
]]>
171
+
</screen>
172
+
</example>
123
173
</para>
124
-
</refsect1><!-- }}} -->
174
+
</refsect1>
125
175

126
176
<refsect1 role="seealso">
127
177
&reftitle.seealso;
128
178
<para>
129
179
<simplelist>
180
+
<member><link linkend="language.types.numeric-strings">Numeric strings</link></member>
130
181
<member><function>ctype_digit</function></member>
131
182
<member><function>is_bool</function></member>
132
183
<member><function>is_null</function></member>
...
...
@@ -135,12 +186,12 @@ NULL is NOT numeric
135
186
<member><function>is_string</function></member>
136
187
<member><function>is_object</function></member>
137
188
<member><function>is_array</function></member>
189
+
<member><function>filter_var</function></member>
138
190
</simplelist>
139
191
</para>
140
192
</refsect1>
141
193

142
194
</refentry>
143
-

144
195
<!-- Keep this comment at the end of the file
145
196
Local variables:
146
197
mode: sgml
147
198