Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How can we know the count/number of elements of an array?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 111
    Comment on it

    Hello Readers

    In php we count the number of elements of an array by two ways:

    1. sizeof()
    2. count()

    sizeof()

    sizeof() function is an alias of count() and it is used to count the elements of an array.

    Syntax of the function sizeof():

    sizeof(array_name, mode)
    in above syntak=x

    array_name: array_name is the first parameter of sizeof() function which is used to specifes the array.

    mode: mode is the second parameter of the array_name function which is used to set the mode of the function (default value is 0).

    Example of sizeof():

    $a[0] = 'History'; 
    $a[1] = 'English';
    $a[2] = 'Maths';
    $a[3] = 'Science';
    $result = sizeof($a);
    echo $result;

    output:

    4


    count()

    count() function work same as a sizeof() function in php. This function returns the number of elements in an array or the count() function is used to count the elements of an array.
    One more interesting point regarding count() function is, if you just pass a simple var instead of an array, count() will return 1.

    Syntax of the count() function:

    count(array_name, mode)

    In above syntax:

    array_name: array_name is the first parameter of count() function which is used to specifes the array.It is a required parameter.

    mode: mode is the second parameter of the count function which is used to set the mode of the function (default value is 0). It is optional parameter.

    Example of count() function:

    $a[0] = 'Geogrphy'; 
    $a[1] = 'English';
    $a[2] = 'Maths';
    $a[3] = 'Science';
    $result = count($a);
    echo $result;

    output:

    4

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: