Array Sum

<?php
	/* Array declaration */
	$array = array(2,3,5,6,8,7,1,0,9,4);
	$sum = 0;
	
	/* Iterate array using 'foreach' */
	foreach($array as $number) {
		$sum += $number;
	}
	echo $sum."<br />";
	
	/* Array declaration */
	$array1 = array(2,3,5,6,8,7,1,0,9,4);
	$array2 = array(1,2,3,4,5,6,7,8,9,0);
	$arr_sum = 0;
	
	/* Loop process upto the length of the array using "for" loop */
	for($i = 0; $i < count($array1); $i++) {
		$arr_sum = $array1[$i] + $array2[$i];
		echo $arr_sum."<br />";
	}
?>



/* Output */
45
3
5
8
10
13
13
8
8
18
4

Comments (0)

  • To add your comment please or

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.

Got It!