Write a program to print the matrix pattern in php language using the for loop
ADVERTISEMENTS
Write a program to print the matrix pattern in php language using the for loop. There are you will learn how to print the matrix pattern in php language using the for loop.
Take an example to print this matrix pattern through a php program:
<?php
// Write a program to print the matrix pattern
// in PHP language using the for loop
$x = 4;
$d = 1;
$m = 0;
$mat = [];
for ( $i = 0; $i < $x; $i++) {
$j = $i;
$k = 0;
for ( $j = $i; $j >= 0; $j--) {
$mat[$k][$j] = $d;
$d++;
$k++;
}
}
for ( $k = 1; $k < $x; $k++) {
$i = $m = $k;
for ( $j = $x-1; $j >= $m; $j--) {
$mat[$i][$j] = $d;
$d++;
$i++;
}
}
echo "\n-----The matrix pattern-----\n\n";
for ( $i = 0; $i < $x; $i++) {
for($j = 0; $j < $x; $j++) {
echo $mat[$i][$j] . "\t";
}
echo "\n";
}
?>
Output:
-----The matrix pattern-----
1 2 4 7
3 5 8 11
6 9 12 14
10 13 15 16
Tags:
# write a program to print concentric rectangular pattern in a 2d matrix in php language
# write a program to print matrix in diagonal pattern in php language
# write a program to print the diagonal number pattern in php language