<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'de',
  ),
  'this' => 
  array (
    0 => 'function.count.php',
    1 => 'count',
    2 => 'Z&auml;hlt alle Elemente eines Arrays oder eines Countable-Objekts',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Array Funktionen',
  ),
  'prev' => 
  array (
    0 => 'function.compact.php',
    1 => 'compact',
  ),
  'next' => 
  array (
    0 => 'function.current.php',
    1 => 'current',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'de',
    'path' => 'reference/array/functions/count.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.count" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">count</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">count</span> &mdash; <span class="dc-title">Zählt alle Elemente eines Arrays oder eines <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>-Objekts</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.count-description">
  <h3 class="title">Beschreibung</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>count</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="class.countable.php" class="type Countable">Countable</a></span>|<span class="type"><a href="language.types.array.php" class="type array">array</a></span></span> <code class="parameter">$value</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$mode</code><span class="initializer"> = <strong><code><a href="array.constants.php#constant.count-normal">COUNT_NORMAL</a></code></strong></span></span>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   Zählt alle Elemente eines Arrays, wenn es mit einem Array verwendet wird.
   Bei Verwendung mit einem Objekt, das die Schnittstelle
   <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span> nutzt, gibt es den Rückgabewert
   der Methode <span class="methodname"><a href="countable.count.php" class="methodname">Countable::count()</a></span> zurück.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.count-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">value</code></dt>
     <dd>
      <p class="para">
       Ein Array oder ein <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>-Objekt.
      </p>
     </dd>
    
    
     <dt><code class="parameter">mode</code></dt>
     <dd>
      <p class="para">
       Wenn der optionale Parameter <code class="parameter">mode</code> auf
       <strong><code><a href="array.constants.php#constant.count-recursive">COUNT_RECURSIVE</a></code></strong> (oder 1) gesetzt ist, wird
       <span class="function"><strong>count()</strong></span> rekursiv durch das Array zählen. Dies kann
       besonders nützlich sein, um alle Elemente eines mehrdimensionalen
       Arrays zu zählen.
      </p>
      <div class="caution"><strong class="caution">Achtung</strong>
       <p class="para">
        <span class="function"><strong>count()</strong></span> kann Rekursion erkennen, um eine
        Endlosschleife zu vermeiden, erzeugt aber jedes Mal, wenn es das tut,
        ein <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> (für den Fall, dass das Array sich
        selbst mehr als einmal enthält) und gibt eine höhere Anzahl zurück,
        als erwartet werden kann.
       </p>
      </div>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.count-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Gibt die Anzahl von Elementen in <code class="parameter">value</code> zurück. Vor
   PHP 8.0.0 wurde <code class="literal">1</code> zurückgegeben, wenn der Parameter
   weder ein <a href="language.types.array.php" class="link">Array</a> noch ein <a href="language.types.object.php" class="link">Objekt</a> mit implementierter
   <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>-Schnittstelle war, es sei denn,
   <code class="parameter">value</code> war null, dann wurde <code class="literal">0</code>
   zurückgegeben.
  </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>Beschreibung</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <span class="function"><strong>count()</strong></span> löst nun einen
       <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> aus, wenn ungültige zählbare Typen an
       den Parameter <code class="parameter">value</code> übergeben werden.
      </td>
     </tr>

     <tr>
      <td>7.2.0</td>
      <td>
       <span class="function"><strong>count()</strong></span> erzeugt nun eine Warnung für ungültige
       zählbare Typen, die an den Parameter <code class="parameter">value</code>
       übergeben wurden.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.count-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="count.example.basic">
    <p><strong>Beispiel #1 <span class="function"><strong>count()</strong></span>-Beispiel</strong></p>
    <div class="example-contents">
<div class="annotation-interactive 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">] = </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">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">]  = </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">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 /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">int(3)
int(3)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="count.example.badexample">
    <p><strong>Beispiel #2 <span class="function"><strong>count()</strong></span>-Beispiel für ein nicht zählbares Array (schlechtes Beispiel - nicht verwenden)</strong></p>
    <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$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">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>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">int(3)

Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable .. on line 12</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="count.example.recursive">
    <p><strong>Beispiel #3 Rekursives <span class="function"><strong>count()</strong></span>-Beispiel</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$food </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'Obst' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'Orange'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Banane'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Apfel'</span><span style="color: #007700">),<br />              </span><span style="color: #DD0000">'Gemüse' </span><span style="color: #007700">=&gt; array(</span><span style="color: #DD0000">'Karotte'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Kohl'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Erbse'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// rekursiv zählen<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">$food</span><span style="color: #007700">, </span><span style="color: #0000BB">COUNT_RECURSIVE</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// normales zählen<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">$food</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">int(8)
int(2)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="count.example.countable">
    <p><strong>Beispiel #4 <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>-Objekt</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">CountOfMethods </span><span style="color: #007700">implements </span><span style="color: #0000BB">Countable<br /></span><span style="color: #007700">{<br />    private function </span><span style="color: #0000BB">someMethod</span><span style="color: #007700">()<br />    {<br />    }<br /><br />    public function </span><span style="color: #0000BB">count</span><span style="color: #007700">(): </span><span style="color: #0000BB">int<br />    </span><span style="color: #007700">{<br />        return </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class_methods</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">));<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">CountOfMethods</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">$obj</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende Ausgabe:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">int(2)</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.count-seealso">
  <h3 class="title">Siehe auch</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.is-array.php" class="function" rel="rdfs-seeAlso">is_array()</a> - Pr&uuml;ft, ob die Variable ein Array ist</span></li>
    <li><span class="function"><a href="function.isset.php" class="function" rel="rdfs-seeAlso">isset()</a> - Pr&uuml;ft, ob eine Variable deklariert ist und sich von null unterscheidet</span></li>
    <li><span class="function"><a href="function.empty.php" class="function" rel="rdfs-seeAlso">empty()</a> - Pr&uuml;ft, ob eine Variable leer ist</span></li>
    <li><span class="function"><a href="function.strlen.php" class="function" rel="rdfs-seeAlso">strlen()</a> - Ermitteln der String-L&auml;nge</span></li>
    <li><span class="function"><a href="function.is-countable.php" class="function" rel="rdfs-seeAlso">is_countable()</a> - Verify that the contents of a variable is a countable value</span></li>
    <li><a href="language.types.array.php" class="link">Arrays</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>