# PHP Program to find the sum of two matrices

April 6, 2022, Learn eTutorial
4406

A matrix is a multidimensional array. For a better understanding of this PHP Matrix addition, we always recommend you to learn the basic topics of  PHP programming listed below:

## How to Calculate The Addition of 2 Matrices?

To find the sum of 2 two-dimensional arrays first we have to find the number of rows and columns in the array and add the numbers in the corresponding indices together. For example, If the matrices are

2  3  8              1  5  1

a   = 3  2  1     b   =  6  1  5

3  5  6               4  1  2

then the result will be:

3  8  9

9  3  6

7  6  8

## How to perform matrix addition using PHP?

In this matrix program, the values are all predefined and cannot be changed. if you need the user to have to provide the input values please refer to this PHP Program to insert values into a matrix.

In this program, we are using the static values which are initialized in the code itself. First, we have to initialize 2 two-dimensional arrays a1 and a2 we have to assign the number of rows and columns into the variable row and col using the built-in function `count(). `Then we have to create an empty array sum[] to store the sum of array a1[] and a2[]. Then assign the value 0 to the variable i and perform the loop until the condition 'i < row' becomes false and increment the value of variable i in every iteration in the block of the loop we have to perform another loop in which we have to assign the value 0 into the variable j and perform the loop until the condition 'j < col' becomes false and increment the value of variable j in every iteration in the loop block we have to assign the calculated result of 'a1[i][j] + a2[i][j]' into the array sum[i][j] and at last print the elements of the two-dimensional array sum[][] using `for loop`

We have covered almost all the matrix programs that include all the matrix operations, please refer to the below programs for more matrix operations and matrix-related programs.

Step 1: Initialize 2 two-dimensional arrays a1[] and a2[]

Step 2: Assign the number of rows and columns into the variable row and col using the built-in function `count()`

Step 3: Print the elements in the array a1[] and a2[]

Step 4: Create an empty array sum[] to store the sum of array a1[] and a2[]

Step 5: Assign the value 0 into the variable i and perform the sub-steps until the condition 'i < row' becomes false and increment the value of variable i in every iteration

(i) Assign the value 0 into the variable j and perform the sub-step until the condition 'j < col' becomes false and increment the value of variable j  in every iteration,

(ii) Assign the calculated result of 'a1[i][j] + a2[i][j]' into the array sum[i][j]

Step 6: Print the elements of the two-dimensional array sum[][] using `for loop`

## PHP Source Code

```                                          ```<?php
\$a1 = array(
array(4, 6, 7),
array(3, 9, 9),
array(5, 4, 8)
);
\$a2 = array(
array(6, 7, 5),
array(9, 2, 1),
array(6, 8, 3)
);
\$row = count(\$a1);
\$col = count(\$a1[0]);
echo "First matrix: \n";
for (\$i = 0; \$i < \$row; \$i++) {
for (\$j = 0; \$j < \$col; \$j++) {
echo \$a1[\$i][\$j] . " ";
}
echo "\n";
}
echo "Second matrix: \n";
for (\$i = 0; \$i < \$row; \$i++) {
for (\$j = 0; \$j < \$col; \$j++) {
echo \$a2[\$i][\$j] . " ";
}
echo "\n";
}
\$sum = array();
for (\$i = 0; \$i < \$row; \$i++) {
for (\$j = 0; \$j < \$col; \$j++) {
\$sum[\$i][\$j] = \$a1[\$i][\$j] + \$a2[\$i][\$j];
}
}
echo "Addition of two matrices: \n";
for (\$i = 0; \$i < \$row; \$i++) {
for (\$j = 0; \$j < \$col; \$j++) {
echo \$sum[\$i][\$j] . " ";
}
echo "\n";
}
?>```
```

## OUTPUT

```First matrix:
4 6 7
3 9 9
5 4 8
Second matrix:
6 7 5
9 2 1
6 8 3