Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 2321

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Count all elements in an array, or something in an object</title>

 </head>
 <body><div class="manualnavbar" 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> &mdash; <span class="dc-title">Count all elements in an array, or something in an object</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.count-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>count</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$array_or_countable</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$mode</code><span class="initializer"> = COUNT_NORMAL</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   Counts all elements in an array, or something 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
    <span class="function"><strong>count()</strong></span> by implementing interface
   <a href="class.countable.html" class="classname">Countable</a>. The interface has exactly one method,
    <span class="methodname"><a href="countable.count.html" class="methodname">Countable::count()</a></span>, which returns the return value for the
    <span class="function"><strong>count()</strong></span> 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" id="refsect1-function.count-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">array_or_countable</code></em></span>
     <dd>

      <p class="para">
       An array or <a href="class.countable.html" class="classname">Countable</a> object.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">mode</code></em></span>
     <dd>

      <p class="para">
       If the optional <em><code class="parameter">mode</code></em> parameter is set to
       <strong><code>COUNT_RECURSIVE</code></strong> (or 1),  <span class="function"><strong>count()</strong></span>
       will recursively count the array.  This is particularly useful for
       counting all the elements of a multidimensional array.
      </p>
      <div class="caution"><strong class="caution">Caution</strong>
       <p class="para">
         <span class="function"><strong>count()</strong></span> can detect recursion to avoid an infinite
        loop, but will emit an <strong><code>E_WARNING</code></strong> every time it
        does (in case the array contains itself more than once) and return a
        count higher than may be expected.
       </p>
      </div>
     </dd>

    </dt>

   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.count-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the number of elements in <em><code class="parameter">array_or_countable</code></em>.
   If the parameter is not an array or not an object with
   implemented <a href="class.countable.html" class="classname">Countable</a> interface,
   <em>1</em> will be returned.
   There is one exception, if <em><code class="parameter">array_or_countable</code></em> is <strong><code>NULL</code></strong>,
   <em>0</em> will be returned.
  </p>
  <div class="caution"><strong class="caution">Caution</strong>
   <p class="para">
     <span class="function"><strong>count()</strong></span> may return 0 for a variable that isn&#039;t set,
    but it may also return 0 for a variable that has been initialized with an
    empty array. Use  <span class="function"><a href="function.isset.html" class="function">isset()</a></span> to test if a variable is set.
   </p>
  </div>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.count-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.2.0</td>
       <td>
        The optional <em><code class="parameter">mode</code></em> parameter was added.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.count-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-5006">
    <p><strong>Example #1  <span class="function"><strong>count()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;=&nbsp;</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">]&nbsp;=&nbsp;</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">]&nbsp;=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">//&nbsp;$result&nbsp;==&nbsp;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">]&nbsp;&nbsp;=&nbsp;</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">]&nbsp;&nbsp;=&nbsp;</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">]&nbsp;=&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">//&nbsp;$result&nbsp;==&nbsp;3<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">//&nbsp;$result&nbsp;==&nbsp;0<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">//&nbsp;$result&nbsp;==&nbsp;1<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5007">
    <p><strong>Example #2 Recursive  <span class="function"><strong>count()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$food&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'fruits'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'orange'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'veggie'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'carrot'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'collard'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'pea'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;recursive&nbsp;count<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$food</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNT_RECURSIVE</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;output&nbsp;8<br /><br />//&nbsp;normal&nbsp;count<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$food</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;output&nbsp;2<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.count-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.is-array.html" class="function" rel="rdfs-seeAlso">is_array()</a> - Finds whether a variable is an array</span></li>
    <li class="member"> <span class="function"><a href="function.isset.html" class="function" rel="rdfs-seeAlso">isset()</a> - Determine if a variable is set and is not NULL</span></li>
    <li class="member"> <span class="function"><a href="function.strlen.html" class="function" rel="rdfs-seeAlso">strlen()</a> - Get string length</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" 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>