1

While and For loops in PHP

Most probably you already know how to code the while and for loops in C, C++ and other lenguages, and fortunately, in PHP is actually the same.

In this article I will give you some examples (only in case you get stuck) of these two loops. So, I am going to start with the while loop

The code in the while loop to print all the even numbers from 0 to 100 is the following

<html>
        <head>
                <title>This is a sample</title>
        </head>
        <body>

                <?php
                        $x = 0;

                        while($x <= 100){
                                if($x%2 == 0){
                                        echo "$x\n\t\t";
                                        $x++;
                                }else{
                                        $x++;
                                }
                        }
                ?>

        </body>
</html>

Something important to keep in mind is that I embedded the php code within a html file. I did it in this way because I am not using a large php code, so I feel pretty comfortable doing in this way.

The result of the code above is the following

<html><head><title>This is a sample</title></head><body>

                0
                2
                4
                6
                8
                10
                12
                14
                16
                18
                20
                22
                24
                26
                28
                30
                32
                34
                36
                38
                40
                42
                44
                46
                48
                50
                52
                54
		56
                58
                60
                62
                64
                66
                68
                70
                72
                74
                76
                78
                80
                82
                84
                86
                88
                90
                92
                94
                96
                98
                100

        </body></html>

Right now I will do the same but with a For loop. The code is the following:

<html>
        <head>
                <title>This is a sample</title>
        </head>
        <body>

                <?php
                        for($x = 0; $x <= 100; $x++){
                                if($x%2 == 0){
                                        echo "$x\n\t\t";
                                        $x++;
                                }else{
                                        $x++;
                                }
                        }
                ?>

        </body>
</html>

And the result will look something like this

<html><head><title>This is a sample</title></head><body>

                0
                2
                4
                6
                8
                10
                12
                14
                16
                18
                20
                22
                24
                26
                28
                30
                32
                34
                36
                38
                40
                42
                44
                46
                48
                50
                52
                54
                56
                58
                60
                62
                64
                66
                68
                70
                72
                74
                76
                78
                80
                82
                84
                86
                88
                90
                92
                94
                96
                98
                100

        </body></html>

As you can see is only the same result but something important is that we use the while loop in circumstances where we do not know when it will finish or we do not know the limit, and we use the for loop when we know what is the limit

Escribe tu comentario
+ 2