How to make a diamond shape with asterisks in PHP

1 Answer

0 votes
$max_size = 11;
$whitespace_size = $max_size/2;
$arr = array();
 
for ($i = 1; $i <= $max_size; $i = $i + 2) 
{
    $whitespace = ($max_size - $i / 2) - $whitespace_size;
    $arr[] = str_pad('', $whitespace, ' ') . str_pad('', $i, '*');
}
 
echo implode("\n", $arr);
echo "\n";
echo implode("\n", array_reverse($arr));
echo "\n";


/*
run: 

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

*/

 



answered Jul 5, 2016 by avibootz
...