PHP Size of Array

Mar 26, 2009 Author: City Hall

function sizeof() in PHP is variety of count(), and they both can using
for counting number of overall elements in array. Argument must be enter
with name. Next PHP script is creating empty array, after that is creating
elements whit data using for cicle. every value of element is writing on
page with foreach function. At the end size of array is аssume to variable
and reasult is writing to page.

 

<html>
	<head>
		<title>Size of Array</title>
	</head>
	<body>
		<?php
			$arr = array();
			for ( $i = 0;  $i < 5; $i++ ){
				
			  $arr[$i] = "This is element with index $i <b r>";
				
			}
		foreach ( $arr  as $var ){
		
			echo ( $var );
			
		}	
			$sizeArr = count( $arr );
		echo ( "Size of array \$arr is $sizeArr " );
		?>
	</body>
</html>

 

Output:
This is element with index 0
This is element with index 1
This is element with index 2
This is element with index 3
This is element with index 4
Size of array $arr is 5


views 3154
  1. Add New Comment