Identity Matrix Example Using PHP

Identity Matrix Example Using PHP

  • PHP
  • 1 min read

The identity matrix is a square matrix where all elements of principal diagonals are 1s, and other elements are 0s. In this tutorial, I am giving an identity matrix example using PHP program.

The below is an example of an Identity matrix:

1 0 0
0 1 0
0 0 1

The above matrix is containing 1s for principal diagonals, and others are 0s.

 

Here is a PHP program for displaying an Identity matrix:

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

Output:

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

In the above PHP program, there are three rows and three columns. When row index number is equal to the column index number, then 1s are assigning here as a value to the array elements, and otherwise, 0s are assigning to other array elements.