PHP Scripting

Array Sum

PHP example for array elements sum and sum of two array

6/18/2020
0 views
php-array-sum.phpPHP
<?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
PHParray sum in PHParray elements sumsum of two array

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com