Php prints a solid and hollow bulb method with a side length of N, solid. Php prints a solid and hollow shape with a side length of N. the example in this article describes how php prints a solid and hollow shape with a side length of N. Share it with you for your reference. Php prints a solid and hollow bulb method with a side length of N, solid
This article describes how to print a solid and hollow bulb shape with a side length of N in php. Share it with you for your reference. The specific analysis is as follows:
Calculation method of solid diamond:
$ N: edge length
$ I: current row, starting from 0
$ Rows: total number of rows
Upper part
Front space = $ n-$ I-1
Character count = $ I * 2 + 1
Lower part
Number of leading spaces = $ I-$ n + 1
Number of characters = ($ rows-$ I) * 2-1
Use str_pad to reduce loops such as for and while.
The code is as follows :/**
* Print solid diamond
* @ Param int $ n edge length. the default value is 5.
* @ Param String $ s: the character displayed. default value *
* @ Return String
*/
Function solidDiamond ($ n = 5, $ s = '*'){
$ Str = '';
// Calculate the total number of rows
$ Rows = $ n * 2-1;
// Calculate the number of rows in a loop *
For ($ I = 0; $ I <$ rows; $ I ++ ){
If ($ I <$ n) {// upper part
$ Str. = str_pad ('', ($ n-$ i-1 ),''). str_pad ('', $ I * 2 + 1, $ s ). "\ r \ n ";
} Else {// Lower part
$ Str. = str_pad ('', ($ I-$ n + 1 ),''). str_pad ('', ($ rows-$ I) * 2-1, $ s ). "\ r \ n ";
}
}
Return $ str;
}
Echo'
'; <Br/> echo solidDiamond (5); <br/> echo' ';
The code is as follows :*
***
*****
*******
*********
*******
*****
***
*
Hollow rhombus computing method:
$ N: edge length
$ I: current row, starting from 0
$ Rows: total number of rows
Upper part
Front space = $ n-$ I-1
Number of blank spaces = $ I * 2 + 1-2
Characters = $ I * 2 + 1-Number of blank spaces
Lower part
Number of leading spaces = $ I-$ n + 1
Number of blank spaces = ($ rows-$ I) * 2-1-2
Number of characters = ($ rows-$ I) * 2-1-Number of blank spaces
The code is as follows :/**
* Print hollow rhombus
* @ Param int $ n edge length. the default value is 5.
* @ Param String $ s: the character displayed. default value *
* @ Return String
*/
Function hollowDiamond ($ n = 5, $ s = '*'){
$ Str = '';
// Calculate the total number of rows
$ Rows = $ n * 2-1;
// Calculate the number of rows in a loop *
For ($ I = 0; $ I <$ rows; $ I ++ ){
If ($ I <$ n) {// upper part
$ Tmp = $ I * 2 + 1;
$ Str. = str_pad ('', ($ n-$ i-1 ),''). str_pad ('', $ tmp-2,'', STR_PAD_BOTH), $ tmp, $ s, STR_PAD_BOTH ). "\ r \ n ";
} Else {// Lower part
$ Tmp = ($ rows-$ I) * 2-1;
$ Str. = str_pad ('', ($ I-$ n + 1 ),''). str_pad ('', $ tmp-2,'', STR_PAD_BOTH), $ tmp, $ s, STR_PAD_BOTH ). "\ r \ n ";
}
}
Return $ str;
}
Echo'
'; <Br/> echo hollowDiamond (5); <br/> echo' ';
The code is as follows :*
**
**
**
**
**
**
**
*
I hope this article will help you with php programming.
Examples in this article describes how php prints a solid and hollow shape with a side length of N. Share it with you for your reference ....