<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.count.php',
    1 => 'count',
    2 => 'Compte tous les &eacute;l&eacute;ments d\'un tableau ou dans un objet Countable',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Fonctions sur les tableaux',
  ),
  'prev' => 
  array (
    0 => 'function.compact.php',
    1 => 'compact',
  ),
  'next' => 
  array (
    0 => 'function.current.php',
    1 => 'current',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Compte tous les éléments d&#039;un tableau ou dans un objet <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span></span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.count-description">
  <h3 class="title">Description</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">
   Compte tous les éléments dans un tableau quand utilisé avec un <a href="language.types.array.php" class="link">tableau</a>.
   Quand utilisé avec un objet qui implémente l&#039;interface
   <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>, ceci retourne la valeur de la
   méthode <span class="methodname"><a href="countable.count.php" class="methodname">Countable::count()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.count-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">value</code></dt>
     <dd>
      <p class="para">
       Un tableau ou un objet <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">mode</code></dt>
     <dd>
      <p class="para">
       Si le paramètre optionnel <code class="parameter">mode</code> vaut
       <strong><code><a href="array.constants.php#constant.count-recursive">COUNT_RECURSIVE</a></code></strong> (ou 1), <span class="function"><strong>count()</strong></span>
       va compter récursivement les tableaux. C&#039;est particulièrement utile
       pour compter tous les éléments d&#039;un tableau multidimensionnel.
      </p>
      <div class="caution"><strong class="caution">Attention</strong>
       <p class="para">
        La fonction <span class="function"><strong>count()</strong></span> peut détecter les récursions
        afin d&#039;éviter les boucles infinies, mais émettra une alerte de type
        <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> à chaque fois qu&#039;une boucle infinie surviendra
        (dans le cas où un tableau se contient lui-même plus d&#039;une fois)
        et retournera un compteur plus grand que l&#039;attendu.
       </p>
      </div>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.count-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Retourne le nombre d&#039;éléments dans <code class="parameter">value</code>.
   Antérieur à PHP 8.0.0, si le paramètre n&#039;est ni un <a href="language.types.array.php" class="link">tableau</a>, ni un <a href="language.types.object.php" class="link">objet</a>
   qui implémente l&#039;interface <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>,
   <code class="literal">1</code> était retourné, sauf si <code class="parameter">value</code>
   était <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, auquel cas <code class="literal">0</code> était retourné.
  </p>
 </div>


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

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <span class="function"><strong>count()</strong></span> lance désormais une <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span>
       pour des types dénombrables invalides passés au paramètre
       <code class="parameter">value</code>.
      </td>
     </tr>

     <tr>
      <td>7.2.0</td>
      <td>
       <span class="function"><strong>count()</strong></span> génère désormais un avertissement pour
       des types dénombrables invalides passés au paramètre
       <code class="parameter">value</code>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.count-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="count.example.basic">
    <p><strong>Exemple #1 Exemple avec <span class="function"><strong>count()</strong></span></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>L&#039;exemple ci-dessus va afficher :</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>Exemple #2 Exemple de <span class="function"><strong>count()</strong></span> avec un argument non Countable|array (contre-exemple - ne pas faire ceci)</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>L&#039;exemple ci-dessus va afficher :</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>Exemple #3 Exemple de récursivité avec <span class="function"><strong>count()</strong></span></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">'fruits' </span><span style="color: #007700">=&gt; 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">=&gt; 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">// count récursif<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">// count normal<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>L&#039;exemple ci-dessus va afficher :</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>Exemple #4 Objet <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span></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 />    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 /></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>L&#039;exemple ci-dessus va afficher :</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">Voir aussi</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> - D&eacute;termine si une variable est un tableau</span></li>
    <li><span class="function"><a href="function.isset.php" class="function" rel="rdfs-seeAlso">isset()</a> - D&eacute;termine si une variable est d&eacute;clar&eacute;e et est diff&eacute;rente de null</span></li>
    <li><span class="function"><a href="function.empty.php" class="function" rel="rdfs-seeAlso">empty()</a> - D&eacute;termine si une variable est vide</span></li>
    <li><span class="function"><a href="function.strlen.php" class="function" rel="rdfs-seeAlso">strlen()</a> - Calcule la taille d'une cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.is-countable.php" class="function" rel="rdfs-seeAlso">is_countable()</a> - V&eacute;rifie si le contenu de la variable est une valeur d&eacute;nombrable</span></li>
    <li><a href="language.types.array.php" class="link">Les tableaux</a></li>
   </ul>
  </p>
 </div>

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