如何在php中循环编号(输入:5)

How to loop with sample input = 5

and the output :

1 2 3 4 5
0 2 3 4 5
0 0 3 4 5
0 0 0 4 5
0 0 0 0 5

PHP:

<?php 
$i=5; 
for($a=1; $a<=$i; $a++){ 
   echo $a." "; 
} 
echo "
"; 
for($a=0; $a<=$i; $a++){ 
  if($a==1){ 
   continue;
  } 
  print "$a "; 
} 
echo "
"; $ex = array(1,2); 
for($a=1; $a<=$i; $a++){ 
  if(in_array($a, $ex)){ 
    continue;
  } 
  print "$a "; 
} 
?>

How to solve this issue?

Think simple

<?php
$input = 5;
for($i = 1; $i <= $input; $i++ ) {
    for($j = 1; $j <= $input; $j++) {
        if( $i > $j) {
            echo "0 ";
        } else {
            echo $j . " ";
         }
    }
    echo "<br>";
}
?>

Using built-in functions, it's easier to read and understand:

$input = 5;
$nums = range(1, $input);
for ($zeros_count = 0; $zeros_count < $input; $zeros_count++) {
    echo str_repeat('0 ', $zeros_count);
    echo implode(' ', array_slice($nums, $zeros_count)) . PHP_EOL;
}