138. PHP Program to print Outer Square elements of a given matrix.
How does this program work?
- In this program we are going to learn about how to print Outer Square elements of a given matrix using PHP.
- Elements are stored in array variable. Here we used 4*4 matrix to display outer square elements of given matrix.
Here is the code
<html>
<head>
<title>PHP Program To print Outer Square of a given matrix</title>
</head>
<body>
<?php
//Initialize A matrix
$a = array
(
array( 1, 2, 3, 4 ),
array( 2, 3, 4, 1 ),
array( 3, 4, 2, 1 ),
array( 4, 3, 2, 1 )
);
$n = count($a);
echo("Original matrix is:<br>"); // To print Original matrix
for($i = 0; $i < $n; $i++) {
for($j = 0; $j < $n; $j++) {
echo ($a[$i][$j] . " ");
}
echo("<br>");
}
echo "Outer square of the given matrix is :"."<br>";
function outer($a, $m, $n)
//To print boundary elements of matrix
{
for ($i = 0; $i < $m; $i++)
{
for ($j = 0; $j < $n; $j++)
{
if ($i == 0)
echo $a[$i][$j], " ";
else if ($i == $m - 1)
echo $a[$i][$j], " ";
else if ($j == 0)
echo $a[$i][$j], " ";
else if ($j == $n - 1)
echo $a[$i][$j], " ";
else
echo " ". " ";
}
echo"<br>";
}
}
outer($a, 4, 4);
?>
</body>
</html>