SoFunction
Updated on 2025-03-10

How to output triangles using for statements

This article example describes the method of using the for statement to output triangles in PHP. Share it for your reference. The specific implementation method is as follows:

<?php
 //phpinfo();
  function Dis($num)
  {
    if($num<=0)
    return;
    $tmpRow=0;
    for($i=1;$i<=$num;$i++)
    {
      $tmpRow=$i<=$num/2 ? $i:$num-$i+1;
      echo str_repeat('*',2*$tmpRow-1).'<br />';
    }
  }
  Dis(19);
?>

I hope this article will be helpful to everyone's PHP programming.