Skip to main content
added 11 characters in body
Source Link

It looks nice. Can you please make me understand how it works

For example

 function f($n){$s='';$b=str_pad('*',($m=$n*2+1),' ',2);for($i=0;$i<$m;++$i)$s.=($i==$n)?str_repeat('*',$m):$b;return chunk_split($s, $m);}

Output

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

Sandbox

Let me write it in a way that is easier to see

$n = 5;
$row_len = $n*2+1;
$output = '';
//pad $n spaces on each side of *
$default = str_pad('*',$row_len,' ',2STR_PAD_BOTH); //is = "     *     "

//loop once for each vertical row (same as width)
for($i=0;$i<$row_len;++$i){
    if($i==$n){
        //do the center line all *'s
        $output .= str_repeat('*',$row_len);//is = "***********"
    }else{
        //use the default line from above
        $output .= $default;
    }
}

//now we have a single line that is the length squared so we can chunk it
//into equal parts, to make a square
echo chunk_split($output, $row_len);  //is = "     *          *     ***********"

Sandbox

Basically we can create the " * " row by using string pad. Because we can pad spaces on both sides of a * up to the length of a row ($n*2)+1.

Then the center row, is just * so we can use string repeat to the length of the row for that line.

Last we take our big huge line of spaces and * and split it into chunks (\n) on the length of our row we want.

It looks nice. Can you please make me understand how it works

For example

 function f($n){$s='';$b=str_pad('*',($m=$n*2+1),' ',2);for($i=0;$i<$m;++$i)$s.=($i==$n)?str_repeat('*',$m):$b;return chunk_split($s, $m);}

Output

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

Sandbox

Let me write it in a way that is easier to see

$n = 5;
$row_len = $n*2+1;
$output = '';
$default = str_pad('*',$row_len,' ',2); //is = "     *     "

for($i=0;$i<$row_len;++$i){
    if($i==$n){
        $output .= str_repeat('*',$row_len);//is = "***********"
    }else{
        $output .= $default;
    }
}

echo chunk_split($output, $row_len);  //is = "     *          *     ***********"

Sandbox

Basically we can create the " * " row by using string pad. Because we can pad spaces on both sides of a * up to the length of a row ($n*2)+1.

Then the center row, is just * so we can use string repeat to the length of the row for that line.

Last we take our big huge line of spaces and * and split it into chunks (\n) on the length of our row we want.

It looks nice. Can you please make me understand how it works

For example

 function f($n){$s='';$b=str_pad('*',($m=$n*2+1),' ',2);for($i=0;$i<$m;++$i)$s.=($i==$n)?str_repeat('*',$m):$b;return chunk_split($s, $m);}

Output

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

Sandbox

Let me write it in a way that is easier to see

$n = 5;
$row_len = $n*2+1;
$output = '';
//pad $n spaces on each side of *
$default = str_pad('*',$row_len,' ',STR_PAD_BOTH); //is = "     *     "

//loop once for each vertical row (same as width)
for($i=0;$i<$row_len;++$i){
    if($i==$n){
        //do the center line all *'s
        $output .= str_repeat('*',$row_len);//is = "***********"
    }else{
        //use the default line from above
        $output .= $default;
    }
}

//now we have a single line that is the length squared so we can chunk it
//into equal parts, to make a square
echo chunk_split($output, $row_len);  //is = "     *          *     ***********"

Sandbox

Basically we can create the " * " row by using string pad. Because we can pad spaces on both sides of a * up to the length of a row ($n*2)+1.

Then the center row, is just * so we can use string repeat to the length of the row for that line.

Last we take our big huge line of spaces and * and split it into chunks (\n) on the length of our row we want.

added 385 characters in body
Source Link

It looks nice. Can you please make me understand how it works

For example

 function f($n){$s='';$b=str_pad('*',($m=$n*2+1),' ',2);for($i=0;$i<$m;++$i)$s.=($i==$n)?str_repeat('*',$m):$b;return chunk_split($s, $m);}

Output

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

Sandbox

Let me write it in a way that is easier to see

$n = 5;
$row_len = $n*2+1;
$output = '';
$default = str_pad('*',$row_len,' ',2); //is = "     *     "

for($i=0;$i<$row_len;++$i){
    if($i==$n){
        $output .= str_repeat('*',$row_len);//is = "***********"
    }else{
        $output .= $default;
    }
}

echo chunk_split($output, $row_len);  //is = "     *          *     ***********"

Sandbox

Basically we can create the " * " row by using string pad. Because we can pad spaces on both sides of a * up to the length of a row ($n*2)+1.

Then the center row, is just * so we can use string repeat to the length of the row for that line.

Last we take our big huge line of spaces and * and split it into chunks (\n) on the length of our row we want.

It looks nice. Can you please make me understand how it works

For example

 function f($n){$s='';$b=str_pad('*',($m=$n*2+1),' ',2);for($i=0;$i<$m;++$i)$s.=($i==$n)?str_repeat('*',$m):$b;return chunk_split($s, $m);}

Output

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

Sandbox

Let me write it in a way that is easier to see

$n = 5;
$row_len = $n*2+1;
$output = '';
$default = str_pad('*',$row_len,' ',2); //"     *     "

for($i=0;$i<$row_len;++$i){
    if($i==$n){
        $output .= str_repeat('*',$row_len);//"***********"
    }else{
        $output .= $default;
    }
}

echo chunk_split($output, $row_len);  //"     *          *     ***********"

Sandbox

It looks nice. Can you please make me understand how it works

For example

 function f($n){$s='';$b=str_pad('*',($m=$n*2+1),' ',2);for($i=0;$i<$m;++$i)$s.=($i==$n)?str_repeat('*',$m):$b;return chunk_split($s, $m);}

Output

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

Sandbox

Let me write it in a way that is easier to see

$n = 5;
$row_len = $n*2+1;
$output = '';
$default = str_pad('*',$row_len,' ',2); //is = "     *     "

for($i=0;$i<$row_len;++$i){
    if($i==$n){
        $output .= str_repeat('*',$row_len);//is = "***********"
    }else{
        $output .= $default;
    }
}

echo chunk_split($output, $row_len);  //is = "     *          *     ***********"

Sandbox

Basically we can create the " * " row by using string pad. Because we can pad spaces on both sides of a * up to the length of a row ($n*2)+1.

Then the center row, is just * so we can use string repeat to the length of the row for that line.

Last we take our big huge line of spaces and * and split it into chunks (\n) on the length of our row we want.

Source Link

It looks nice. Can you please make me understand how it works

For example

 function f($n){$s='';$b=str_pad('*',($m=$n*2+1),' ',2);for($i=0;$i<$m;++$i)$s.=($i==$n)?str_repeat('*',$m):$b;return chunk_split($s, $m);}

Output

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

Sandbox

Let me write it in a way that is easier to see

$n = 5;
$row_len = $n*2+1;
$output = '';
$default = str_pad('*',$row_len,' ',2); //"     *     "

for($i=0;$i<$row_len;++$i){
    if($i==$n){
        $output .= str_repeat('*',$row_len);//"***********"
    }else{
        $output .= $default;
    }
}

echo chunk_split($output, $row_len);  //"     *          *     ***********"

Sandbox