'Problem during creating Pyramid Star Patterns in PHP [duplicate]

Question : enter image description here

My Code :

   <html\>


  
    Enter the number of rows:   
      
  


<?php   
    if($_POST)  
    {     
        $row = $_POST['row'];   
    
        if(!is_numeric($row))  
        {  
            echo "Make sure you enter a NUMBER";  
            return;   
        }   
    
        else  
        {  
            for($row=0;$row<=1;$row++){ 
    for($j=0;$j<=$row;$j++){ echo "#"; } echo ""; }}}?>

The problem is it's showing only two rows

I expected as shown in the photo

php


Solution 1:[1]

$row = 10;
for($i=0;$i<=$row;$i++){ 
for($j=0;$j<=$i;$j++){ echo "#"; } echo "<br>"; }

Solution 2:[2]

You are overriding the $row variable. Also, you don't need the else since you are returning in case the if(!is_numeric) is true;

This should do it.

if(isset($_POST['row'])) {     
    $rows = $_POST['row'];   

    if(!is_numeric($rows))  
    {  
        echo "Make sure you enter a NUMBER";  
        return;   
    }

    for($row=0;$row<=$rows;$row++){ 

        for($j=0;$j<=$row;$j++){ 
            echo "#"; 
        } 
        
        echo "\n";
    }
    
}

Solution 3:[3]

You can make use of the PHP function str_repeat to simplify the script. This would only take 1 loop, so you don't get confused with the variable names.

$row = 10;
$i   = 1;
while ($i <= $row)
{
    echo str_repeat("#", $i); echo "\n";
    $i ++;
}

Working Example here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shoyeb Sheikh
Solution 2 Cornel Raiu
Solution 3