📄 function.count.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Count elements in an array, or properties in an object</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.compact.html">compact</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.current.html">current</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.count" class="refentry"> <div class="refnamediv"> <h1 class="refname">count</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">count</span> — <span class="dc-title">Count elements in an array, or properties in an object</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>count</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$var</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$mode</tt></span> ] )</div> <p class="para rdfs-comment"> Counts elements in an array, or properties in an object. </p> <p class="para"> For objects, if you have <a href="ref.spl.html" class="link">SPL</a> installed, you can hook into <b>count()</b> by implementing interface <i>Countable</i>. The interface has exactly one method, <b>count()</b>, which returns the return value for the <b>count()</b> function. </p> <p class="para"> Please see the <a href="language.types.array.html" class="link">Array</a> section of the manual for a detailed explanation of how arrays are implemented and used in PHP. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">var</tt></i></span> <dd> <p class="para"> The array. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">mode</tt></i></span> <dd> <p class="para"> If the optional <i><tt class="parameter">mode</tt></i> parameter is set to <b><tt>COUNT_RECURSIVE</tt></b> (or 1), <b>count()</b> will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. The default value for <i><tt class="parameter">mode</tt></i> is <i>0</i>. <b>count()</b> does not detect infinite recursion. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the number of elements in <i><tt class="parameter">var</tt></i>, which is typically an <a href="language.types.array.html" class="type array">array</a>, since anything else will have one element. </p> <p class="para"> If <i><tt class="parameter">var</tt></i> is not an array or an object with implemented <i>Countable</i> interface, <i>1</i> will be returned. There is one exception, if <i><tt class="parameter">var</tt></i> is <b><tt>NULL</tt></b>, <i>0</i> will be returned. </p> <div class="caution"><b class="caution">Caution</b> <p class="para"> <b>count()</b> may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use <a href="function.isset.html" class="function">isset()</a> to test if a variable is set. </p> </div> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.2.0</td> <td colspan="1" rowspan="1" align="left"> The optional <i><tt class="parameter">mode</tt></i> parameter was added. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>count()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">] = </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">] = </span><span style="color: #0000BB">3</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">] = </span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// $result == 3<br /><br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">] = </span><span style="color: #0000BB">7</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">5</span><span style="color: #007700">] = </span><span style="color: #0000BB">9</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">[</span><span style="color: #0000BB">10</span><span style="color: #007700">] = </span><span style="color: #0000BB">11</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// $result == 3<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// $result == 0<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// $result == 1<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 Recursive <b>count()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$food </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'fruits' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'orange'</span><span style="color: #007700">, </span><span style="color: #DD0000">'banana'</span><span style="color: #007700">, </span><span style="color: #DD0000">'apple'</span><span style="color: #007700">),<br /> </span><span style="color: #DD0000">'veggie' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">, </span><span style="color: #DD0000">'collard'</span><span style="color: #007700">, </span><span style="color: #DD0000">'pea'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// recursive count<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$food</span><span style="color: #007700">, </span><span style="color: #0000BB">COUNT_RECURSIVE</span><span style="color: #007700">); </span><span style="color: #FF8000">// output 8<br /><br />// normal count<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$food</span><span style="color: #007700">); </span><span style="color: #FF8000">// output 2<br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.is-array.html" class="function" rel="rdfs-seeAlso">is_array()</a></li> <li class="member"><a href="function.isset.html" class="function" rel="rdfs-seeAlso">isset()</a></li> <li class="member"><a href="function.strlen.html" class="function" rel="rdfs-seeAlso">strlen()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.compact.html">compact</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.current.html">current</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -