reference/array/functions/count.xml
3ef3e56c80f7ae6b6a7291b7eb57dc6250b50801
...
...
@@ -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,7 +128,7 @@ 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
133
<programlisting role="php">
145
134
<![CDATA[
...
...
@@ -186,7 +175,7 @@ Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Cou
186
175
</example>
187
176
</para>
188
177
<para>
189
-
<example>
178
+
<example xml:id="count.example.recursive">
190
179
<title>Recursive <function>count</function> example</title>
191
180
<programlisting role="php">
192
181
<![CDATA[
...
...
@@ -195,14 +184,52 @@ $food = array('fruits' => array('orange', 'banana', 'apple'),
195
184
'veggie' => array('carrot', 'collard', 'pea'));
196
185

197
186
// recursive count
198
-
echo count($food, COUNT_RECURSIVE); // output 8
187
+
var_dump(count($food, COUNT_RECURSIVE));
199
188

200
189
// normal count
201
-
echo count($food); // output 2
190
+
var_dump(count($food));
202
191

203
192
?>
204
193
]]>
205
194
</programlisting>
195
+
&example.outputs;
196
+
<screen role="php">
197
+
<![CDATA[
198
+
int(8)
199
+
int(2)
200
+
]]>
201
+
</screen>
202
+
</example>
203
+
</para>
204
+
<para>
205
+
<example xml:id="count.example.countable">
206
+
<title><interfacename>Countable</interfacename> object</title>
207
+
<programlisting role="php">
208
+
<![CDATA[
209
+
<?php
210
+
class CountOfMethods implements Countable
211
+
{
212
+
private function someMethod()
213
+
{
214
+
}
215
+

216
+
public function count(): int
217
+
{
218
+
return count(get_class_methods($this));
219
+
}
220
+
}
221
+

222
+
$obj = new CountOfMethods();
223
+
var_dump(count($obj));
224
+
?>
225
+
]]>
226
+
</programlisting>
227
+
&example.outputs;
228
+
<screen role="php">
229
+
<![CDATA[
230
+
int(2)
231
+
]]>
232
+
</screen>
206
233
</example>
207
234
</para>
208
235
</refsect1>
...
...
@@ -216,6 +243,7 @@ echo count($food); // output 2
216
243
<member><function>empty</function></member>
217
244
<member><function>strlen</function></member>
218
245
<member><function>is_countable</function></member>
246
+
<member><link linkend="language.types.array">Arrays</link></member>
219
247
</simplelist>
220
248
</para>
221
249
</refsect1>
222
250