Site icon T4Tutorials.com

Code of Addition of the matrix in PHP 

Code of Addition of the matrix in PHP

In this tutorial, we will try to learn the followings;

  1. Code of Addition of the matrix in PHP
  2. Addition of matrix with form
  3. Addition of matrix using do while loop in PHP
  4. Addition of matrix using  while loop in PHP

Code of Addition of the matrix in PHP

//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

//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

Figure: Output of addition of matrix in PHP with form

Addition of matrix using do while loop in PHP

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

Figure: Output of Addition of matrix using do while loop

Addition of matrix using  while loop in PHP

<?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

Figure: Addition of matrix using  while loop in PHP
Exit mobile version