PHP Program to Display The Matrix

PHP Program to Display The Matrix

  • PHP
  • 1 min read

Here is a PHP program to display the matrix as follows:

0 0 1
0 1 0
1 0 0

Here in the above matrix, there are three rows and three columns and when row index number+column index number=number of rows-1 then one will be assigned as a value to the array element and otherwise 0 will be assigned to other elements.

 

PHP Program to Display The Matrix

<html>
    <body>
        <?php
echo "<b>The matrix is given below:-</b>"."<br>";
$a=array(array());
$rows=3;
$cols=3;
for($i=0;$i<$rows;$i=$i+1)
{
    for($j=0;$j<$cols;$j=$j+1)
    {
        if($i+$j==$rows-1) 
        {
            $a[$i][$j]=1;
        }
        else
        {
            $a[$i][$j]=0;
        }
        echo $a[$i][$j]." ";
    }
    echo "<br>";
}
        ?>
    </body>
</html>

Output:

The matrix is given below:-
0 0 1 
0 1 0 
1 0 0

Here $i is the row index number and $j is the column index number and if their addition is equal to the number of rows-1(here $rows is the number of rows of the array $a), then 1 will be assigned to array element(here $a[$i][$j] is the array element)and 0 will be assigned to other elements.