PHP星号三角形对齐

I'm new to web programming and recently started getting into PHP. I wrote a little code to produce a "right angle triangle". I recognize that using &nbsp is part of the solution but I've placed it in every possible place with no luck so any advice would be appreciated..Below you will find the coding/current output/desired output:

$x = 10;
while ( $x >= 1 ) {
    $y=1;
    while ($y <= $x) {
        echo "*";
        ++$y;
    }
    echo "<br/>";
    --$x;
}

output:
**********
*********
********
*******
******
*****
****
***
**
*


desire output:
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *

You want 10 characters per line, n asterix and 10 - n spaces. Knowing this, you just need to add another loop inside to control how many spaces to output!

Something as simple as:

$x = 10;
while ( $x >= 1 ) {
    $spaces = 1;
    while($spaces <= 10 - $x)
    {
        echo "&nbsp";
        ++$spaces;
    }
    $y=1;
    while ($y <= $x) {
        echo "*";
        ++$y;
    }
    echo "<br/>";
    --$x;
}

Here's my suggestion; without whiles, but using a for loop (and str_repeat()s).

echo '<pre>'; // just here for display formatting

// $x = current number of *
// $t = total number of positions
for( $x = $t = 10; $x > 0; $x-- )
{
    // repeat '&nbsp;' $t - $x times
    // repeat '*' $x times
    // append '<br>'
    echo str_repeat( '&nbsp;', $t - $x ) . str_repeat( '*', $x ) . '<br>';
}

And with a while loop:

echo '<pre>'; // just here for display formatting

$x = $t = 10;
while( $x > 0 )
{
    echo str_repeat( '&nbsp;', $t - $x ) . str_repeat( '*', $x ) . '<br>';
    --$x;
}

To echo your original triangle, just switch the str_repeat()s.

<?php

$num = 10;
$char = '*';
$string = str_repeat($char, $num);

for ($i = 1; $i <= $num; $i++)
{
    printf("%{$num}s
", $string);
    $string = substr_replace($string, '', -1);
}

?>

Wrap in <pre> tags if you want easy formatting.

I hope this code may be helpfull for you

new code to create star triangle using call_user_func() and for loop()

Star triangle

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

Php code

<?php
/**
  | Code for display star triangle using for loop 
  | date : 2016-june-10
  | @uther : Aman kumar
  */

$sum = "*";
for($i=0;$i<=5;$i++)
{
    call_user_func(function($sum) { echo $sum, "<br/>"; }, str_repeat($sum,$i));
}
?>