Home » PHP

PHP sizeof() function with example

PHP sizeof() function: Here, we are going to learn about the sizeof() function with example in PHP?
Submitted by IncludeHelp, on February 14, 2019

PHP sizeof() function

sizeof() function is an alias of count() function, it is used to get the total number of elements of an array.

Syntax:

    sizeof(array, [count_mode])

Here,

  • array is the name of an input array
  • count_mode is an optional parameter and it's default value is 0, it has two values
    0 – It does not count all elements of a multidimensional array
    1 – It counts all elements of a multidimensional array

Examples:

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

    Output:
    arr1 has 5 elements

PHP code: Using single dimensional array

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

Output

arr1 has 5 elements
arr2 has 5 elements

PHP code: Using multidimensional array

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

    $len = sizeof($students);
    print ("sizeof value = $len (sizeof_mode is not provided)\n");

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

Output

sizeof value = 2 (sizeof_mode is not provided)
sizeof value = 2 (sizeof_mode is set to 0)
sizeof value = 6 (sizeof_mode is set to 1)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.