Home Â» PHP Â» PHP Program to Display The Boundary Elements of a Given Matrix

PHP Program to Display The Boundary Elements of a Given Matrix

Here is a PHP program to display the boundary elements of a given matrix.Â Boundary elements are the outsider elements of a matrix. Here the elements are of minimum and maximum rows with minimum columns and maximum columns.

The below is an example for boundary elements of a matrix given below:

```1	2	3	4	1	2	3	4
2	3	4	5  To   2			5
3	4	5	6	3			6
4	5	6	7	4	5	6	7```

Here there are 4 rows and 4 columns. So boundary elements will be of the first row and last row (here 4th row) with the first column and last column (here 4th column).

PHP Program to Display The Boundary Elements of a Given Matrix

```<html>
<body>
<?php
echo "<b>The matrix is given below:-</b>"."<br>";
\$a=array(array());
\$rows=4;
\$cols=4;
for(\$i=0;\$i<\$rows;\$i=\$i+1)
{
for(\$j=0;\$j<\$cols;\$j=\$j+1)
{
\$a[\$i][\$j]=\$i+\$j+1;
echo \$a[\$i][\$j]." ";
}
echo "<br>";
}
echo "<b>The matrix with boundary elements is given below:-</b>"."<br>";//here <b></b>is using to define a text in bold style
for(\$i=0;\$i<\$rows;\$i=\$i+1)
{
for(\$j=0;\$j<\$cols;\$j=\$j+1)
{
if(\$i==0||\$i==\$rows-1||\$j==0||\$j==\$cols-1)
{
echo \$a[\$i][\$j]." ";
}
else
{
echo "&nbsp&nbsp&nbsp";
}
}
echo "<br>"; //here this is creating a new line
}
?>
</body>
</html>```

Output:

```The matrix is given below:-
1 2 3 4Â
2 3 4 5Â
3 4 5 6Â
4 5 6 7Â
The matrix with boundary elements is given below:-
1 2 3 4Â
2 Â Â Â Â 5Â
3 Â Â Â Â 6Â
4 5 6 7```

Here we have created a 4*4 matrix with a statement (\$a[\$i][\$j]=\$i+\$j+1) and then displayed that matrix. After displaying the matrix, we have displayed the boundary elements with spaces of the original matrix based on a condition (here either \$i==0(row index number=0) or \$i==\$rows-1 or \$j==0 (column index number=0),\$j==\$cols-1). Here \$nbsp is creating a space.