<?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 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.count.php',
    1 => 'count',
    2 => '配列または Countable オブジェクトに含まれるすべての要素の数を数える',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => '配列 関数',
  ),
  'prev' => 
  array (
    0 => 'function.compact.php',
    1 => 'compact',
  ),
  'next' => 
  array (
    0 => 'function.current.php',
    1 => 'current',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    '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">配列または <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">説明</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">
   配列の場合は、配列の全ての要素を数えます。
   <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span>
   インターフェイスを実装したオブジェクトの場合は、
   <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">パラメータ</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">value</code></dt>
     <dd>
      <p class="para">
       配列あるいは <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">
       オプションの<code class="parameter">mode</code> 引数が <strong><code><a href="array.constants.php#constant.count-recursive">COUNT_RECURSIVE</a></code></strong>
       (または 1) にセットされた場合、<span class="function"><strong>count()</strong></span>
       は再帰的に配列をカウントします。
       これは多次元配列の全ての要素をカウントするといった場合に特に有効です。
      </p>
      <div class="caution"><strong class="caution">警告</strong>
       <p class="para">
        <span class="function"><strong>count()</strong></span> は、再帰を検出して無限ループを回避するようになっています。
        しかしその場合 (配列の中に自分自身が複数回登場する場合) は毎回 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>
        を発行し、期待する結果より大きい数を返します。
       </p>
      </div>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.count-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   <code class="parameter">value</code> の要素の数を返します。
   PHP 8.0.0 より前のバージョンでは、
   パラメータが配列でもなく
   <span class="interfacename"><a href="class.countable.php" class="interfacename">Countable</a></span> インターフェイスを
   実装したオブジェクトでもない場合、<code class="literal">1</code> が返されていました。
   ただし、<code class="parameter">value</code> が <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> の場合、
   <code class="literal">0</code> が返されていました。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.count-changelog">
  <h3 class="title">変更履歴</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>バージョン</th>
      <th>説明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">value</code> パラメータに不正な型を渡した場合に、
       <span class="classname"><a href="class.typeerror.php" class="classname">TypeError</a></span> をスローするようになりました。
      </td>
     </tr>

     <tr>
      <td>7.2.0</td>
      <td>
       <code class="parameter">value</code> パラメータに不正な型を渡した場合に、
       警告を発生させるようになりました。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.count-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="count.example.basic">
    <p><strong>例1 <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>上の例の出力は以下となります。</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>例2 Countable|array でない値を渡した場合の <span class="function"><strong>count()</strong></span>  の例 (悪い例です - 真似しないでね！)</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>上の例の出力は以下となります。</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>例3 再帰的な <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">// recursive count<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">// normal count<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>上の例の出力は以下となります。</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>例4 <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 /><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>上の例の出力は以下となります。</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">参考</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> - 変数が配列かどうかを検査する</span></li>
    <li><span class="function"><a href="function.isset.php" class="function" rel="rdfs-seeAlso">isset()</a> - 変数が宣言されていること、そして null とは異なることを検査する</span></li>
    <li><span class="function"><a href="function.empty.php" class="function" rel="rdfs-seeAlso">empty()</a> - 変数が空であるかどうかを検査する</span></li>
    <li><span class="function"><a href="function.strlen.php" class="function" rel="rdfs-seeAlso">strlen()</a> - 文字列の長さを得る</span></li>
    <li><span class="function"><a href="function.is-countable.php" class="function" rel="rdfs-seeAlso">is_countable()</a> - 引数が、数えられる値かどうかを調べる</span></li>
    <li><a href="language.types.array.php" class="link">Arrays</a></li>
   </ul>
  </p>
 </div>


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