PHP sizeof() function with example

PHP sizeof() function with example

Syntax

sizeof(array, [count_mode])

Example

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

    Output:
    arr1 has 5 elements

PHP code: Using a 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 a 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)

Recent Posts

error: Alert: Content is protected!!