<HTML ><HEAD ><TITLE >count</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Array Functions" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="compact" HREF="function.compact.html"><LINK REL="NEXT" TITLE="current" HREF="function.current.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.compact.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.current.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.count" ></A >count</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN8513" ></A ><P > (PHP 3, PHP 4 )</P >count -- Count elements in a variable</DIV ><DIV CLASS="refsect1" ><A NAME="AEN8516" ></A ><H2 >Description</H2 >int <B CLASS="methodname" >count</B > ( mixed var)<BR ></BR ><P > Returns the number of elements in <TT CLASS="parameter" ><I >var</I ></TT >, which is typically an <A HREF="language.types.array.html" ><B CLASS="type" >array</B ></A > (since anything else will have one element). </P ><P > If <TT CLASS="parameter" ><I >var</I ></TT > is not an array, <TT CLASS="literal" >1</TT > will be returned (exception: <TT CLASS="literal" >count(<TT CLASS="constant" ><B >NULL</B ></TT >)</TT > equals <TT CLASS="literal" >0</TT >). </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > <B CLASS="function" >count()</B > may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use <A HREF="function.isset.html" ><B CLASS="function" >isset()</B ></A > to test if a variable is set. </P ></TD ></TR ></TABLE ></DIV ><P > Please see the <A HREF="language.types.array.html" >Arrays</A > section of the manual for a detailed explanation of how arrays are implemented and used in PHP. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN8540" ></A ><P ><B >Example 1. <B CLASS="function" >count()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$a[0] = 1; $a[1] = 3; $a[2] = 5; $result = count ($a); // $result == 3 $b[0] = 7; $b[5] = 9; $b[10] = 11; $result = count ($b); // $result == 3;</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > The <A HREF="function.sizeof.html" ><B CLASS="function" >sizeof()</B ></A > function is an <A HREF="aliases.html" >alias</A > for <B CLASS="function" >count()</B >. </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.is-array.html" ><B CLASS="function" >is_array()</B ></A >, <A HREF="function.isset.html" ><B CLASS="function" >isset()</B ></A >, and <A HREF="function.strlen.html" ><B CLASS="function" >strlen()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.compact.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.current.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >compact</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >current</TD ></TR ></TABLE ></DIV ></BODY ></HTML >