Home >Backend Development >PHP Tutorial >How to print a solid and hollow diamond shape with side length N_PHP tutorial
This article mainly introduces how to print a solid and hollow diamond with side length N in php Methods and examples analyze the techniques of drawing graphics using PHP loop statements. Friends in need can refer to it
The example in this article describes how PHP prints a solid and hollow diamond shape with side length N. Share it with everyone for your reference. The specific analysis is as follows:
Solid diamond calculation method:
$n: side length
$i: current line, starting from 0
$rows: Total number of rows
Upper part
Number of preceding spaces =$n-$i-1
Number of characters=$i*2+1
Lower part
Number of preceding spaces =$i-$n+1
Number of characters=($rows-$i)*2-1
Use str_pad to reduce for/while loops
The code is as follows:
The code is as follows:
Hollow diamond calculation method:
$n: side length
$i: current line, starting from 0
$rows: Total number of rows
Upper part
Number of preceding spaces =$n-$i-1
Number of empty spaces =$i*2+1-2
Number of characters = $i*2+1 - number of empty spaces
Lower part
Number of preceding spaces =$i-$n+1
Number of empty spaces = ($rows-$i)*2-1-2
Number of characters = ($rows-$i)*2-1 - Number of empty spaces
The code is as follows:
The code is as follows:
I hope this article will be helpful to everyone’s PHP programming design.