fork download
  1. <?php
  2.  
  3. $N=5; //size matrix
  4.  
  5. $pl=$N+1; //for 1st diagonal
  6. $mn=$N-1; //for 2nd diagonal
  7.  
  8. echo 'Horizontal lines'.PHP_EOL;
  9. for($i=1; $i<=$N*$N; $i++)
  10. echo $i % $N == 0 ? $i.PHP_EOL : $i.' ';
  11.  
  12. echo PHP_EOL.'Vertical lines'.PHP_EOL;
  13. for($i=1; $i<=$N; $i++) {
  14. $vrt=$i;
  15. for($j=1; $j<=$N; $j++) {
  16. echo $j == $N ? $vrt.PHP_EOL : $vrt.' ';
  17. $vrt+=$N;
  18. }
  19. }
  20.  
  21. echo PHP_EOL.'First diagonal'.PHP_EOL;
  22. $frt=1;
  23. for($i=1; $i<=$N; $i++) {
  24. echo $frt.' ';
  25. $frt+=$pl;
  26. }
  27.  
  28. echo PHP_EOL.'Second diagonal'.PHP_EOL;
  29. $scn=$N;
  30. for($i=1; $i<=$N; $i++) {
  31. echo $scn.' ';
  32. $scn+=$mn;
  33. }
  34.  
  35.  
Success #stdin #stdout 0.03s 52432KB
stdin
Standard input is empty
stdout
Horizontal lines
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Vertical lines
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25

First diagonal
1 7 13 19 25 
Second diagonal
5 9 13 17 21