PHP Program to find Matrix Addition

Last Updated:

Find Matrix Addition

In the following example, we will add the two given matrices (two-dimensional arrays).

Example

PHP Compiler
<?php $arr1 = array( array(1, 1, 1), array(1, 1, 1), array(1, 1, 1), ); $arr2 = array( array(2, 2, 2), array(2, 2, 2), array(2, 2, 2), ); echo "Matrix A (3 x 3): <br>"; for($i=0; $i<3; $i++) { for($j=0; $j<3; $j++) echo $arr1[$i][$j] . " "; echo "<br>"; } echo "<br>Matrix B (3 x 3): <br>"; for($i=0; $i<3; $i++) { for($j=0; $j<3; $j++) echo $arr2[$i][$j] . " "; echo "<br>"; } echo "<br>Matrix Additon (A + B): <br>"; for($i=0; $i<3; $i++) { for($j=0; $j<3; $j++) { $arr3[$i][$j] = $arr1[$i][$j] + $arr2[$i][$j]; echo $arr3[$i][$j] . " "; } echo "<br>"; } ?>

Share this Page

Meet the Author