reference/array/functions/count.xml
2e60c5134e7a847c99f81eb3f7ecee1f5efeeace
2e60c5134e7a847c99f81eb3f7ecee1f5efeeace
...
...
@@ -4,7 +4,7 @@
4
4
<refentry xml:id="function.count" xmlns="http://docbook.org/ns/docbook">
5
5
<refnamediv>
6
6
<refname>count</refname>
7
-
<refpurpose>Count all elements in an array, or something in an object</refpurpose>
7
+
<refpurpose>Counts all elements in an array or in a <interfacename>Countable</interfacename> object</refpurpose>
8
8
</refnamediv>
9
9
10
10
<refsect1 role="description">
...
...
@@ -15,20 +15,9 @@
15
15
<methodparam choice="opt"><type>int</type><parameter>mode</parameter><initializer><constant>COUNT_NORMAL</constant></initializer></methodparam>
16
16
</methodsynopsis>
17
17
<para>
18
-
Counts all elements in an array, or something in an object.
19
-
</para>
20
-
<para>
21
-
For objects, if you have
22
-
<link linkend="ref.spl">SPL</link> installed, you can hook into
23
-
<function>count</function> by implementing interface
24
-
<classname>Countable</classname>. The interface has exactly one method,
25
-
<methodname>Countable::count</methodname>, which returns the return value for the
26
-
<function>count</function> function.
27
-
</para>
28
-
<para>
29
-
Please see the <link linkend="language.types.array">Array</link>
30
-
section of the manual for a detailed explanation of how arrays
31
-
are implemented and used in PHP.
18
+
Counts all elements in an array when used with an array. When used with an
19
+
object that implements the <interfacename>Countable</interfacename> interface, it returns
20
+
the return value of the method <methodname>Countable::count</methodname>.
32
21
</para>
33
22
</refsect1>
34
23
...
...
@@ -40,7 +29,7 @@
40
29
<term><parameter>value</parameter></term>
41
30
<listitem>
42
31
<para>
43
-
An array or <classname>Countable</classname> object.
32
+
An array or <interfacename>Countable</interfacename> object.
44
33
</para>
45
34
</listitem>
46
35
</varlistentry>
...
...
@@ -71,11 +60,11 @@
71
60
&reftitle.returnvalues;
72
61
<para>
73
62
Returns the number of elements in <parameter>value</parameter>.
74
-
When the parameter is neither an array nor an object with
75
-
implemented <classname>Countable</classname> interface,
76
-
<literal>1</literal> will be returned.
77
-
There is one exception, if <parameter>value</parameter> is &null;,
78
-
<literal>0</literal> will be returned.
63
+
Prior to PHP 8.0.0, if the parameter was neither an &array; nor an &object; that
64
+
implements the <interfacename>Countable</interfacename> interface,
65
+
<literal>1</literal> would be returned,
66
+
unless <parameter>value</parameter> was &null;, in which case
67
+
<literal>0</literal> would be returned.
79
68
</para>
80
69
</refsect1>
81
70
...
...
@@ -112,7 +101,7 @@
112
101
<refsect1 role="examples">
113
102
&reftitle.examples;
114
103
<para>
115
-
<example>
104
+
<example xml:id="count.example.basic">
116
105
<title><function>count</function> example</title>
117
106
<programlisting role="php">
118
107
<![CDATA[
...
...
@@ -139,9 +128,9 @@ int(3)
139
128
</example>
140
129
</para>
141
130
<para>
142
-
<example>
131
+
<example xml:id="count.example.badexample">
143
132
<title><function>count</function> non Countable|array example (bad example - don't do this)</title>
144
-
<programlisting role="php">
133
+
<programlisting role="php" annotations="non-interactive">
145
134
<![CDATA[
146
135
<?php
147
136
$b[0] = 7;
...
...
@@ -159,26 +148,6 @@ var_dump(count(false));
159
148
<screen role="php">
160
149
<![CDATA[
161
150
int(3)
162
-
int(0)
163
-
int(1)
164
-
]]>
165
-
</screen>
166
-
&example.outputs.72;
167
-
<screen role="php">
168
-
<![CDATA[
169
-
int(3)
170
-
171
-
Warning: count(): Parameter must be an array or an object that implements Countable in … on line 12
172
-
int(0)
173
-
174
-
Warning: count(): Parameter must be an array or an object that implements Countable in … on line 14
175
-
int(1)
176
-
]]>
177
-
</screen>
178
-
&example.outputs.8;
179
-
<screen role="php">
180
-
<![CDATA[
181
-
int(3)
182
151
183
152
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable .. on line 12
184
153
]]>
...
...
@@ -186,7 +155,7 @@ Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Cou
186
155
</example>
187
156
</para>
188
157
<para>
189
-
<example>
158
+
<example xml:id="count.example.recursive">
190
159
<title>Recursive <function>count</function> example</title>
191
160
<programlisting role="php">
192
161
<![CDATA[
...
...
@@ -195,14 +164,52 @@ $food = array('fruits' => array('orange', 'banana', 'apple'),
195
164
'veggie' => array('carrot', 'collard', 'pea'));
196
165
197
166
// recursive count
198
-
echo count($food, COUNT_RECURSIVE); // output 8
167
+
var_dump(count($food, COUNT_RECURSIVE));
199
168
200
169
// normal count
201
-
echo count($food); // output 2
170
+
var_dump(count($food));
202
171
203
172
?>
204
173
]]>
205
174
</programlisting>
175
+
&example.outputs;
176
+
<screen role="php">
177
+
<![CDATA[
178
+
int(8)
179
+
int(2)
180
+
]]>
181
+
</screen>
182
+
</example>
183
+
</para>
184
+
<para>
185
+
<example xml:id="count.example.countable">
186
+
<title><interfacename>Countable</interfacename> object</title>
187
+
<programlisting role="php">
188
+
<![CDATA[
189
+
<?php
190
+
class CountOfMethods implements Countable
191
+
{
192
+
private function someMethod()
193
+
{
194
+
}
195
+
196
+
public function count(): int
197
+
{
198
+
return count(get_class_methods($this));
199
+
}
200
+
}
201
+
202
+
$obj = new CountOfMethods();
203
+
var_dump(count($obj));
204
+
?>
205
+
]]>
206
+
</programlisting>
207
+
&example.outputs;
208
+
<screen role="php">
209
+
<![CDATA[
210
+
int(2)
211
+
]]>
212
+
</screen>
206
213
</example>
207
214
</para>
208
215
</refsect1>
...
...
@@ -216,6 +223,7 @@ echo count($food); // output 2
216
223
<member><function>empty</function></member>
217
224
<member><function>strlen</function></member>
218
225
<member><function>is_countable</function></member>
226
+
<member><link linkend="language.types.array">Arrays</link></member>
219
227
</simplelist>
220
228
</para>
221
229
</refsect1>
222
230