Sophie

Sophie

distrib > Mageia > 7 > aarch64 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 2836

php-manual-en-7.2.11-1.mga7.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, PHP 7)</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>
<code class="parameter">array_or_countable</code></dt>

     <dd>

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

    
    
     <dt>
<code class="parameter">mode</code></dt>

     <dd>

      <p class="para">
       If the optional <code class="parameter">mode</code> 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>

    
   </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 <code class="parameter">array_or_countable</code>.
   When the parameter is neither an array nor 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 <code class="parameter">array_or_countable</code> is <strong><code>NULL</code></strong>,
   <em>0</em> will be returned.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.count-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-6222">
    <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">var_dump</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 /><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">var_dump</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 /><br /></span><span style="color: #0000BB">var_dump</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 /><br /></span><span style="color: #0000BB">var_dump</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: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int(3)
int(3)

Warning: count(): Parameter must be an array or an object that implements Countable in … on line 12 // as of PHP 7.2
int(0)

Warning: count(): Parameter must be an array or an object that implements Countable in … on line 14 // as of PHP 7.2
int(1)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6223">
    <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 changelog" id="refsect1-function.count-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>7.2.0</td>
      <td>
       <span class="function"><strong>count()</strong></span> will now yield a warning on invalid countable types 
       passed to the <code class="parameter">array_or_countable</code> parameter.
      </td>
     </tr>

    </tbody>
   
  </table>

 </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.empty.html" class="function" rel="rdfs-seeAlso">empty()</a> - Determine whether a variable is empty</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>