reference/var/functions/is-numeric.xml
0817d5b2835f8a47314823338d983fa2c5005dfc
...
...
@@ -12,15 +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
-
whitespace, optional
20
-
sign, any number of digits, optional decimal part and optional exponential part.
21
-
Thus <literal>+0123.45e6</literal> is a valid numeric value. Hexadecimal (e.g.
22
-
<literal>0xf4c3b00c</literal>) and binary (e.g. <literal>0b10100111001</literal>)
23
-
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>.
24
20
</para>
25
21
</refsect1>
26
22

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

51
71
<refsect1 role="examples">
52
72
&reftitle.examples;
53
73
<para>
...
...
@@ -70,7 +90,8 @@ $tests = array(
70
90
"not numeric",
71
91
array(),
72
92
9.1,
73
-
null
93
+
null,
94
+
'',
74
95
);
75
96

76
97
foreach ($tests as $element) {
...
...
@@ -101,42 +122,62 @@ array (
101
122
) is NOT numeric
102
123
9.1 is numeric
103
124
NULL is NOT numeric
125
+
'' is NOT numeric
104
126
]]>
105
127
</screen>
106
128
</example>
107
129
</para>
108
-
</refsect1>
109
130

110
-
<refsect1 role="changelog"><!-- {{{ -->
111
-
&reftitle.changelog;
112
131
<para>
113
-
<informaltable>
114
-
<tgroup cols="2">
115
-
<thead>
116
-
<row>
117
-
<entry>&Version;</entry>
118
-
<entry>&Description;</entry>
119
-
</row>
120
-
</thead>
121
-
<tbody>
122
-
<row>
123
-
<entry>7.0.0</entry>
124
-
<entry>
125
-
Strings in hexadecimal (e.g. <literal>0xf4c3b00c</literal>) notation are
126
-
no longer regarded as numeric strings, i.e.
127
-
<function>is_numeric</function> returns &false; now.
128
-
</entry>
129
-
</row>
130
-
</tbody>
131
-
</tgroup>
132
-
</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>
133
173
</para>
134
-
</refsect1><!-- }}} -->
174
+
</refsect1>
135
175

136
176
<refsect1 role="seealso">
137
177
&reftitle.seealso;
138
178
<para>
139
179
<simplelist>
180
+
<member><link linkend="language.types.numeric-strings">Numeric strings</link></member>
140
181
<member><function>ctype_digit</function></member>
141
182
<member><function>is_bool</function></member>
142
183
<member><function>is_null</function></member>
...
...
@@ -145,12 +186,12 @@ NULL is NOT numeric
145
186
<member><function>is_string</function></member>
146
187
<member><function>is_object</function></member>
147
188
<member><function>is_array</function></member>
189
+
<member><function>filter_var</function></member>
148
190
</simplelist>
149
191
</para>
150
192
</refsect1>
151
193

152
194
</refentry>
153
-

154
195
<!-- Keep this comment at the end of the file
155
196
Local variables:
156
197
mode: sgml
157
198