Skip to content
Advertisement

How can i make looping on php like this

The code should be like this

01
002
0003
00004
000005
6000000
70000000
800000000
9000000000
10000000000

This my code

for ( $i = 1; $i <= 10; $i++ ){
        echo "<br>";
        for ( $j = 1; $j <= $i; $j++ ){
            for( $k = 1; $k >= $j; $k-- ){
                echo $i;
            }
            if( $i >= $k ) {
                echo "0";
            }
        }
        if ($i >= 6) {
            echo "0";
        }else {
            echo $i;

        }
    }

I cant make like the result, can you help me thanks.. no error but weird result

Advertisement

Answer

You can do it like this (without using any in-build functions):

for ( $i = 1; $i <= 10; $i++ ){
    if($i<=5){
        for ( $j = 1; $j <= $i; $j++ ){
            echo "0";
        }
        echo $i;
    }
    if($i>5){
        echo $i;
        for ( $j = 1; $j <= $i; $j++ ){
            echo "0";
        }
    }
    echo "<br>";
}

The output will be :

01
002
0003
00004
000005
6000000
70000000
800000000
9000000000
100000000000

You can test this code in here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement