PHP loops through the inverted pyramid

Source: Internet
Author: User
Turned out a few months ago when learning PHP, wrote down the Pyramid, stare at 1 more than a few minutes to see understand. Also depressed at that time how to write out, the original is a regular, and now the code refining the next, write down. Code this thing, really is afraid to forget-_-!!!

Code:

function A ($n) {
$p = ($n + 1)/2; Calculate the middle of the line number
for ($i =1; $i <= $n; $i + +) {
if ($i < $p) {//Traverse upper part
$m = $i-1; Top half: Number of * numbers per line
$SP = ($n-$m)/2; Half the number of spaces
$i 1 = $m; Number of asterisks
for ($j =1; $j <= $sp; $j + +) {echo "";} Space
for ($j =1; $j <= $i 1; $j + +) {echo "*";}//Print * #
for ($j =1; $j <= $sp; $j + +) {echo "";} Space
echo "
";
}else{
if ($i = = $p) {//Traverse middle row: The number of the middle is exactly equal to the number of rows
for ($j =1; $j <= $n; $j + +) {echo "*";}//Print * #
echo "
";
}
else{//traversing the lower half
$i 2 = ($n +1-$i)-1; * Numbered (regular-symmetrical)
$SP = ($n-$i 2)/2; Half the number of spaces
for ($j =1; $j <= $sp; $j + +) {echo "";} Space
for ($j =1; $j <= $i 2; $j + +) {echo "*";}//Print * #
for ($j =1; $j <= $sp; $j + +) {echo "";} Space
echo "
";
}
}
}
}
A (9);
?>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PHP loop traversal inverted pyramid, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.