Home  >  Article  >  Backend Development  >  How to use loop to output diamond shape in php

How to use loop to output diamond shape in php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-28 17:13:156550browse

How to use loop to output diamond shape in php

Ideas:

(1) Write the code to print a solid pyramid.

(2) Modify the code and empty the pyramid.

(3) Modify the code and invert the pyramid.

(4) Modify the inverted code and empty the pyramid.

(5) Modify the code and parameterize the specific numbers.

1. Write the code to print the pyramid with solid gold characters:

<?php
    //$n=5;
    for($i=1;$i<=5;$i++){
        //打印空格
        for($j=1;$j<=5-$i;$j++){
            echo " ";
        }
        //打印*号
        for($k=1;$k<=2*$i-1;$k++){
            echo "*";
        }
        echo "<br/>";
    }
?>

Related recommendations: "php Getting Started Tutorial"

2. Improve Code, empty the pyramid.

<?php
    //$n=5;
    for($i=1;$i<=5;$i++){
        //打印空格
        for($j=1;$j<=5-$i;$j++){
            echo " ";
        }
    //打印*号
    for($k=1;$k<=2*$i-1;$k++){
        //打印第一行后最后一行都打*连接
        if($i==1 || $i==5){
            echo "*";
        }else{
        //怎么打空格和*号的问题
            if($k==1 || $k==2*$i-1){
                echo "*";
            }else{
                echo " ";
            }
        }
    }
    echo "<br/>";
    }
?>

3. Modify the code and turn the pyramid upside down.

<?php
    //$n=5;
    for($i=1;$i<=5;$i++){
        //打印空格
        for($j=1;$j<=5-$i;$j++){
            echo " ";
        }
        //打印*号
        for($k=1;$k<=2*$i-1;$k++){
            //打印第一行后最后一行都打*连接
            if($i==1 || $i==5){
                    echo "*";
            }else{
                //怎么打空格和*号的问题
                if($k==1 || $k==2*$i-1){
                    echo "*";
                }else{
                    echo " ";
                }
            }
        }
    echo "<br/>";
    }
    //倒转
    for($i=5;$i>=0;$i--){
        //打印空格
        for($j=0;$j<=5-$i;$j++){
            echo " ";
        }
        //打印*号
        for($k=1;$k<=2*$i-3;$k++){
            echo "*";
        }
    echo "<br/>";
    }
?>

4. Modify the inverted code, empty the pyramid, remove the * sign in the middle, and modify the code here (if($i==1 || $i==5){ / /Remove $i==5 and short the middle).

<?php
    //$n=5;
    for($i=1;$i<=5;$i++){
        //打印空格
        for($j=1;$j<=5-$i;$j++){
            echo " ";
        }
        //打印*号
        for($k=1;$k<=2*$i-1;$k++){
        //打印第一行后最后一行都打*连接($i==1 || $i==5) 
            if($i==1){ //去掉$i==5 把中间抛空
                echo "*";
            }else{
                //怎么打空格和*号的问题
                if($k==1 || $k==2*$i-1){
                    echo "*";
                }else{
                    echo " ";
            }
        }
    }
    echo "<br/>";
    }
    //倒转 抛空
    for($i=5;$i>=0;$i--){
        //打印空格
        for($j=0;$j<=5-$i;$j++){
            echo " ";
        }
        //打印*号
        for($k=1;$k<=2*$i-3;$k++){
            //怎么打空格和*号的问题
            if($k==2*$i-3 || $k==1){
                echo "*";
            }else{
                echo " ";    
            }
        }
    echo "<br/>";
    }
?>

5. Modify the code and parameterize the specific number: change the specific number 5 to $n=5 instead, and then set the value of $n, which can be enlarged or reduced at will.

<?php
    $n=5;
    for($i=1;$i<=$n;$i++){
        //打印空格
        for($j=1;$j<=$n-$i;$j++){
            echo " ";
        }
        //打印*号
        for($k=1;$k<=2*$i-1;$k++){
        //打印第一行后最后一行都打*连接($i==1 || $i==5) 
            if($i==1){ //去掉$i==5 把中间抛空
                echo "*";
            }else{
                //怎么打空格和*号的问题
                if($k==1 || $k==2*$i-1){
                    echo "*";
                }else{
                    echo " ";
                }
        }
    }
    echo "<br/>";
    }
    //倒转 抛空
    for($i=$n;$i>=0;$i--){
        //打印空格
        for($j=0;$j<=$n-$i;$j++){
            echo " ";
        }
        //打印*号
        for($k=1;$k<=2*$i-3;$k++){
            //怎么打空格和*号的问题
            if($k==2*$i-3 || $k==1){
                echo "*";
            }else{
                echo " ";    
            }
        }
    echo "<br/>";
    }
?>

Rendering:

How to use loop to output diamond shape in php

The above is the detailed content of How to use loop to output diamond shape in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn