What is the difference between count or sizeof function in PHP? explain with example

According to PHP official documentation, there is no difference between count() or sizeof() function in PHP, which means both are the same, the sizeof() function is just the alias of count function i.e. the sizeof() uses the same as count function underneath. Both are used to count elements in an array.sizeof() function is an alias of count() function used in PHP. count() function is faster and butter than sizeof().

count() method: 

The count() method is used to calculate all the elements in the array or any other countable object. It can be used for both uni-dimensional as well as multi-dimensional arrays. 

count(array)

Example:-


<?php
        $parts = array(

                "Ram" => array(
                "SSD",
                "Processor"
            ),
                "Key" => array(
                "Monitor"  
            ),
                "Mouse" => array(
                "Cebal"
            )
        );
        
        print_r($parts);
        print("<br>");
        
        echo "Sub elements of an array: "
            . count($parts) . "<br>";
        echo "All elements of an array: "
            . count($parts, 1);
        
?>

Output:-

sizeof() method: 

The sizeof() method is used to calculate all the elements present in an array or any other countable object. It can be used for both uni-dimensional as well as multi-dimensional arrays. 

sizeof(arr, mode)

Example:-

<?php
    $parts = array(

        "Computer" => array(
        "SSD",
        "Processor"
    ),
        "Keyboard" => array(
        "Monitor"  
    ),
        "Mouse" => array(
        "CPU"
    )
);
    
    print_r($parts);
    print("<br>");
    
    echo "Sub elements of an array: "
        . sizeof($parts) . "<br>";
    echo "All elements of an array: "
        . sizeof($parts, 1);
 
?>

Output:-

Difference between sizeof() and count() methods:

  • The sizeof() method takes a longer execution time.
  • The sizeof() method is an alias of the count() method.
count() sizeof()
1. The count() returns the number of elements in the array.The sizeof() function is used to return the number of elements in an array.
2. Its syntax is -:
count(array, mode)
Its syntax is -:
sizeof(array, mode)
3. Its return value is of integer type.Its return value is of integer type.
4. The count() function may return 0 for a variable that is not set.This function is an alias of count() function.
5. It is supported in PHP version 4.0+It is supported in PHP version 4.0+

Related Posts

PHP – strip_tags() Replace with Spaces Example

We are going to learn php strip tags with spaces. You will learn how to replace php Strip_tags with space. In this article, we will replace php…

How to Get Duplicate Values from Array in PHP?

We will learn php array get duplicate values. we will help you to give an example of how to get duplicate values from array in php. you…

PHP explode() Function Example Tutorial

A PHP explode function example is given in this article. I will demonstrate how to use PHP’s explode string to array function. This is a basic example…

How to Convert Array to String in PHP?

We will cover the example of converting an array to a string in PHP in this lesson. We’ll examine an example of converting an array to a…

How Can I Convert a String in PHP Into an Array?

We will go over how to transform a string into an array in PHP in this little tutorial. Describe how to convert a string value into an…

How to Use the array_merge() Function in PHP?

We will go over the simple and quick method to use the PHP array_merge() function example in this brief tutorial. Let’s talk about the PHP example of…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x