PHP count() function with example

PHP check() work: Here, we will find out about the tally() work with the example in PHP?

PHP check() work

check() work” is utilized to get them all to outnumber of components of an exhibit.

Syntax:

    count(array, [count_mode])

Here,

  • cluster is the name of an info exhibit
  • count_mode is a discretionary parameter and its default value is 0, it has two values
  • 0 – It doesn’t check all components of a multidimensional exhibit
  • 1 – It checks all components of a multidimensional exhibit

Example:

    Input:
    $arr1 = array("101", "102", "103", "104", "105");

    Output:
    arr1 has 5 elements

PHP code: Using single-dimensional cluster

<?php    
    $arr1 = array("101", "102", "103", "104", "105");
    $arr2 = array("Amit", "Abhishek", "Prerana", "Aleesha", "Prem");
	
	$len = count($arr1);
	print ("arr1 has $len elements\n");
	$len = count($arr2);
	print ("arr2 has $len elements\n");	
?>

Output

arr1 has 5 elements
arr2 has 5 elements

PHP code: Using multidimensional cluster

<?php    
    $students = array(
        "101" => array(
                "name" => "Amit",
                "age" => 21,
            ),
        "102" => array(
                "name" => "Abhi",
                "age" => 20,
            )
        );

    $len = count($students);
    print ("count value = $len (count_mode is not provided)\n");

    $len = count($students, 0);
    print ("count value = $len (count_mode is set to 0)\n");    
    
    $len = count($students, 1);
    print ("count value = $len (count_mode is set to 1)\n");    
?>

Output

count value = 2 (count_mode is not provided)
count value = 2 (count_mode is set to 0)
count value = 6 (count_mode is set to 1)

Leave a Comment

error: Alert: Content is protected!!