Php prints a solid and hollow water-shaped method with a side length of N, solid _ PHP Tutorial

Source: Internet
Author: User
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'

'; &lt;Br/&gt; echo solidDiamond (5); &lt;br/&gt; 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'

'; &lt;Br/&gt; echo hollowDiamond (5); &lt;br/&gt; 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 ....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.