I have a php code but don’t know how to show it in HTML table. I created matrix tables to add and multiply but don’t know how to put results in HTML table. I tried to put the code between script tags but it didn’t work. My matrix func basically generates 4 by 4 matrices with random integers and generates two matrices called A and B. then I created two other functions to add and multiply these matrices but I didn’t print them out in HTML table.
My code:
// creating function addMatrix which takes two numbers and returns a new matrix
function addMatrix($A, $B)
{
// declaring $ADD as a empty array
$ADD = array();
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// adding $A[$i][$j] to $B[$i][$j] and store the result in $ADD[$i][$j]
$ADD[$i][$j] = $A[$i][$j] + $B[$i][$j];
}
}
// return $ADD
return $ADD;
}
// creating function mulMatrix which takes two numbers and returns a new matrix
function mulMatrix($A, $B)
{
// declaring $MUL as a empty array
$MUL = array();
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// assign 0 in $MUL[$i][]
$MUL[$i][] = 0;
// iterate from k is equal to 0, k is less than 4 and increment k by 1
for ($k = 0; $k < 4; $k++) {
// multiply $ADD[$i][$k] with $B[$k][$j] then add it in $MUL[$i][$j] and store it
// in $MUL[$i][$j]
$MUL[$i][$j] += $A[$i][$k] * $B[$k][$j];
}
}
}
// return $MUL
return $MUL;
}
// generate matrix by calling generateMatrix and store it in $A
$A = generateMatrix();
// generate matrix by calling generateMatrix and store it in $B
$B = generateMatrix();
// add matrix by calling addMatrix and store it in $ADD
$ADD = addMatrix($A, $B);
// mul matrix by calling mulMatrix and store it in $MUL
$MUL = mulMatrix($A, $B);
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $A[$i][$j] with element td
echo "<td>" . $A[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tag table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'B'</th>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $A[$i][$j] with element td
echo "<td>" . $B[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A' + 'B'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $ADD[$i][$j] with element td
echo "<td>" . $ADD[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";
// create element br
echo "<br>";
// create element table and th
echo "<table border='1' cellspacing='0' cellpadding='10'><th colspan=4>Matrix 'A' * 'B'</th>";
// iterate from i is equal to 0, i is less than 4 and increment i by 1
for ($i = 0; $i < 4; $i++) {
// create element tr
echo "<tr>";
// iterate from j is equal to 0, j is less than 4 and increment j by 1
for ($j = 0; $j < 4; $j++) {
// display value of $MUL[$i][$j] with element td
echo "<td>" . $MUL[$i][$j] . "</td>";
}
// create element end tr table
echo "</tr>";
}
// create element end tr table
echo "</table>";here