Triangle of asterisks in php

395

I am having problems creating a triangle in php. This is my code.

for($i = 0; $i <= $input; $i++) {
    for($j = 1; $j <= $i; $j++) {
        echo " $char &nbsp";
    }
    echo "<br>";
}

The result is this.

*  
*   *  
*   *   *  
*   *   *   *  
*   *   *   *   *  

But what I'm trying to do is this.

                *  
            *   *  
        *   *   *  
    *   *   *   *  
*   *   *   *   *  

What should I change with my codes?

369

Answer

Solution:

There is a native PHP function str_pad where you can say how to pad the string in one of directions:STR_PAD_RIGHT,STR_PAD_BOTH ,STR_PAD_LEFT

<?php
$char = '*'; // the character to print
$input = 10; // number of rows
$padding = STR_PAD_BOTH; // STR_PAD_RIGHT,  STR_PAD_BOTH , STR_PAD_LEFT

for($i = 0; $i <= $input; $i++) {
    $stars = '';
    for($j = 1; $j <= $i; $j++) {
        $stars .= " $char";
    }
    echo PHP_EOL . str_pad($stars, $input*2, ' ', $padding);
}

See it live: https://eval.in/927835

Here was usedSTR_PAD_BOTH:

          *         
         * *        
        * * *       
       * * * *      
      * * * * *     
     * * * * * *    
    * * * * * * *   
   * * * * * * * *  
  * * * * * * * * * 
 * * * * * * * * * *

And hereSTR_PAD_LEFT:

                   *
                 * *
               * * *
             * * * *
           * * * * *
         * * * * * *
       * * * * * * *
     * * * * * * * *
   * * * * * * * * *
 * * * * * * * * * *
323

Answer

Solution:

You have to make sure space is printed the correct number of times.

So first we will loop through all the white spaces, from 1 to $input - $i. here we will print the white space. Then we will loop through the astricks the number of $i times.

Note: we are using &nbsp two times for alignment purpose with astrick

Here is the code:

for($i = 0; $i <= $input; $i++) {
    for($j = 1; $j <= $input - $i; $j++) {
        echo " &nbsp&nbsp";
    }
    for($j = 1; $j <= $i; $j++) {
        echo " $char";
    }
    echo "<br>";
}
695

Answer

Solution:

Create your main character string and "padding" string separately, then echo the concatenated result:

for( $i=0; $i<$input; $i++ ){

    // number of asterisks we need for this iteration
    $chars='';
    for( $j=0; $j<=$i; $j++ ){
        $chars.="$char &nbsp;";
    }

    // add spaces for alignment
    $padding='';
    for( $j=$i; $j<$input; $j++ ){
        $padding.="&nbsp; &nbsp;";
    }

    // output "padding" first, then our asterisks string
    echo $padding.$chars."<br />";

}

People are also looking for solutions to the problem: javascript - Passing PHP $_POST to Ajax Call

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.