 # PHP Program to check whether the matrix is an identity matrix or not

## What is an identity matrix?

The identity matrix is a square matrix of any order in which the diagonal(when the index of row and column are the same) elements are all 1, and all other elements in the matrix must be 0. For example,

[ 1 0 0 ]

[ 0 1 0 ]

[ 0 0 1 ]

In the above matrix, we can see that all the diagonal values are 1 and others are 0 so we can say that this is an identity matrix.

## How to check whether the matrix is an identity matrix or not using PHP?

In this program, we are checking that the entered matrix is an identity matrix or not, for that we are using the static values which are initialized in the code itself. First, we have to initialize two-dimensional arrays a[] then we have to assign the number of rows and columns into the variable row and col using the built-in function `count().`After that, we have to create a variable flag with a value trueThen assign the value 0 into the variable i and perform the loop until the condition 'i < row' becomes false and increment the value of variable i in every iteration in the block of the loop we have to perform another loop in that we have to assign the value 0 into the variable j and perform the loop until the condition 'j < col' becomes false and increment the value of variable j in every iteration in the loop block we have to check the conditions 'i == j' and 'a[i][j] != 1' if both are true assign the value false to the variable flag and after that check the conditions 'i != j' and 'a[i][j] != 0' if both are true assign the value false to the variable flag. After the completion of the loop, we have to check the value of the variable flag and if it is true Print the matrix is an identity matrix otherwise Print the matrix is not an identity matrix.

### ALGORITHM

Step 1: Initialize two-dimensional arrays a[]

Step 2: Assign the number of rows and columns into the variable row and col using the built-in function `count()`

Step 3: Print the elements in the array a[]

Step 4: Create variables flag with the value true

Step 5: Assign the value 0 into the variable i and perform the sub-step until the condition 'i < row' becomes false and increment the value of variable i in every iteration

(i) Assign the value 0 into the variable j and perform the sub-step until the condition 'j < col' becomes false and increment the value of variable j in every iteration

(a) Check the conditions 'i == j' and 'a[i][j] != 1' if both are true assign the value false to the variable flag

(b) Check the conditions 'i != j' and 'a[i][j] != 0' if both are true assign the value false to the variable flag

Step 6: Check the value of the variable flag if it is true Print the matrix is an identity matrix otherwise Print the matrix is not an identity matrix

## PHP Source Code

```                                          ```<?php
\$a = array(
array(1, 0, 0),
array(0, 1, 0),
array(0, 0, 1)
);
\$flag = true;
\$row = count(\$a);
\$col = count(\$a);
echo "The matrix: \n";
for (\$i = 0; \$i < \$row; \$i++) {
for (\$j = 0; \$j < \$col; \$j++) {
echo \$a[\$i][\$j] . " ";
}
echo "\n";
}
if (\$row != \$col) {
echo "Matrix should be a square matrix ";
} else {
for (\$i = 0; \$i < \$row; \$i++) {
for (\$j = 0; \$j < \$col; \$j++) {
if (\$i == \$j && \$a[\$i][\$j] != 1) {
\$flag = false;
break;
}
if (\$i != \$j && \$a[\$i][\$j] != 0) {
\$flag = false;
break;
}
}
}
if (\$flag)
echo "The matrix is an identity matrix";
else
echo "The matrix is not an identity matrix";
}
?>```
```

## OUTPUT

```The matrix:
1 0 0
0 1 0
0 0 1
The matrix is an identity matrix```
VIEW ALL
VIEW ALL
VIEW ALL