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>), Binary (e.g. <literal>0b10100111001</literal>),
22
-
Octal (e.g. <literal>0777</literal>) 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,16 +83,22 @@ $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
-
9.1
92
+
9.1,
93
+
null,
94
+
'',
68
95
);
69
96

70
97
foreach ($tests as $element) {
71
98
if (is_numeric($element)) {
72
-
echo "'{$element}' is numeric", PHP_EOL;
99
+
echo var_export($element, true) . " is numeric", PHP_EOL;
73
100
} else {
74
-
echo "'{$element}' is NOT numeric", PHP_EOL;
101
+
echo var_export($element, true) . " is NOT numeric", PHP_EOL;
75
102
}
76
103
}
77
104
?>
...
...
@@ -81,50 +108,76 @@ foreach ($tests as $element) {
81
108
<screen>
82
109
<![CDATA[
83
110
'42' is numeric
84
-
'1337' is numeric
85
-
'1337' is numeric
86
-
'1337' is numeric
87
-
'1337' is numeric
88
-
'1337' is numeric
111
+
1337 is numeric
112
+
1337 is numeric
113
+
1337 is numeric
114
+
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
89
120
'not numeric' is NOT numeric
90
-
'Array' is NOT numeric
91
-
'9.1' is numeric
121
+
array (
122
+
) is NOT numeric
123
+
9.1 is numeric
124
+
NULL is NOT numeric
125
+
'' is NOT numeric
92
126
]]>
93
127
</screen>
94
128
</example>
95
129
</para>
96
-
</refsect1>
97
130

98
-
<refsect1 role="changelog"><!-- {{{ -->
99
-
&reftitle.changelog;
100
131
<para>
101
-
<informaltable>
102
-
<tgroup cols="2">
103
-
<thead>
104
-
<row>
105
-
<entry>&Version;</entry>
106
-
<entry>&Description;</entry>
107
-
</row>
108
-
</thead>
109
-
<tbody>
110
-
<row>
111
-
<entry>7.0.0</entry>
112
-
<entry>
113
-
Strings in hexadecimal (e.g. <literal>0xf4c3b00c</literal>) notation are
114
-
no longer regarded as numeric strings, i.e.
115
-
<function>is_numeric</function> returns &false; now.
116
-
</entry>
117
-
</row>
118
-
</tbody>
119
-
</tgroup>
120
-
</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>
121
173
</para>
122
-
</refsect1><!-- }}} -->
174
+
</refsect1>
123
175

124
176
<refsect1 role="seealso">
125
177
&reftitle.seealso;
126
178
<para>
127
179
<simplelist>
180
+
<member><link linkend="language.types.numeric-strings">Numeric strings</link></member>
128
181
<member><function>ctype_digit</function></member>
129
182
<member><function>is_bool</function></member>
130
183
<member><function>is_null</function></member>
...
...
@@ -133,12 +186,12 @@ foreach ($tests as $element) {
133
186
<member><function>is_string</function></member>
134
187
<member><function>is_object</function></member>
135
188
<member><function>is_array</function></member>
189
+
<member><function>filter_var</function></member>
136
190
</simplelist>
137
191
</para>
138
192
</refsect1>
139
193

140
194
</refentry>
141
-

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