Code of Addition of the matrix in PHP
In this tutorial, we will try to learn the followings;
- Code of Addition of the matrix in PHP
- Addition of matrix with form
- Addition of matrix using do while loop in PHP
- Addition of matrix using while loop in PHP
Code of Addition of the matrix in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//addition of matrix <?php $array1=array('0'=>array('0'=>1,'1'=>2,'2'=>3),'1'=>array('0'=>4,'1'=>5,'2'=>6),'2'=>array('0'=>7,'1'=>8,'2'=>9)); $array2=array('0'=>array('0'=>2,'1'=>4,'2'=>8),'1'=>array('0'=>3,'1'=>6,'2'=>9),'2'=>array('0'=>4,'1'=>8,'2'=>12)); $result=array(); for($x=0;$x<=2;$x++) { for($y=0;$y<=2;$y++) { $result[$x][$y]=$array1[$x][$y]+$array2[$x][$y]; } echo "<pre>"; echo "matrix"." "; print_r($result); //echo $result; }?> |
Output
Addition of matrix with form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
//addition of matrix with form <html <body> <form method="post" action="addition of matrix with form..php"> Array1 <br> Enter Value: <input type="text" name="1"><br> Enter Value: <input type="text" name="2"><br> Enter Value: <input type="text" name="3"><br> Enter Value: <input type="text" name="4"><br> Enter Value: <input type="text" name="5"><br> Enter Value: <input type="text" name="6"><br> Enter Value: <input type="text" name="7"><br> Enter Value: <input type="text" name="8"><br> Enter Value: <input type="text" name="9"><br> Array2 <br> Enter Value: <input type="text"
name="10"><br> Enter Value: <input type="text" name="11"><br> Enter Value: <input type="text" name="12"><br> Enter Value: <input type="text" name="13"><br> Enter Value: <input type="text" name="14"><br> Enter Value: <input type="text" name="15"><br> Enter Value: <input type="text" name="16"><br> Enter Value: <input type="text" name="17"><br> Enter Value: <input type="text" name="18"><br> <input type="submit"value="addition-of-matrix" name="addition"> </form> <?php $array1=array(array(3),array(3)); $array2=array(array(3),array(3)); //$array3=array(array(3),array(3)); if (isset($_POST['addition'])){ $array1[0][0]=$_POST['1']; $array1[0][1]=$_POST['2']; $array1[0][2]=$_POST['3']; $array1[1][0]=$_POST['4']; $array1[1][1]=$_POST['5']; $array1[1][2]=$_POST['6']; $array1[2][0]=$_POST['7']; $array1[2][1]=$_POST['8']; $array1[2][2]=$_POST['9']; $array2[0][0]=$_POST['10']; $array2[0][1]=$_POST['11']; $array2[0][2]=$_POST['12']; $array2[1][0]=$_POST['13']; $array2[1][1]=$_POST['14']; $array2[1][2]=$_POST['15']; $array2[2][0]=$_POST['16']; $array2[2][1]=$_POST['17']; $array2[2][2]=$_POST['18']; $result=array(); for($x=0;$x<=2;$x++) { for($y=0;$y<=2;$y++) { $result[$x][$y]=$array1[$x][$y]+$array2[$x][$y]; } echo "<pre>"; echo "matrix"." "; print_r($result); //echo $result; }}?> </body> </html> |
Output

Addition of matrix using do while loop in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
addition of matrix ...do while loop <?php $array1=array('0'=>array('0'=>1,'1'=>2,'2'=>3),'1'=>array('0'=>4,'1'=>5,'2'=>6),'2'=>array('0'=>7,'1'=>8,'2'=>9)); $array2=array('0'=>array('0'=>2,'1'=>4,'2'=>8),'1'=>array('0'=>3,'1'=>6,'2'=>9),'2'=>array('0'=>4,'1'=>8,'2'=>12)); $result=array(); $x=0; do { for($y=0;$y<=2;$y++) { $result[$x][$y]=$array1[$x][$y]+$array2[$x][$y]; $x++;} }while($x<=2); echo "matrix"; print_r($result); //echo $result;</pre> ?> |
Output

Addition of matrix using while loop in PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $array1=array('0'=>array('0'=>1,'1'=>2,'2'=>3),'1'=>array('0'=>4,'1'=>5,'2'=>6),'2'=>array('0'=>7,'1'=>8,'2'=>9)); $array2=array('0'=>array('0'=>2,'1'=>4,'2'=>8),'1'=>array('0'=>3,'1'=>6,'2'=>9),'2'=>array('0'=>4,'1'=>8,'2'=>12)); $result=array(); $x=0; while($x<=2) { for($y=0;$y<=2;$y++) { $result[$x][$y]=$array1[$x][$y]+$array2[$x][$y]; } $x++; } echo "<pre>"; echo "matrix"." "; print_r($result); ?> |
Output
