Tutorial Loops in PHP

Lipaydi

1337
VIP
Jul 23, 2015
44
64
112
Loops allow you to repeat a block of code as many times as you want. There are 4 loops in PHP, each actually doing the same thing technically, just different. The most useful is the While and for loop.

Loops are both fun and one of the most useful expressions. Even if there are risks such as infinite loops, PHP's default runtime limit eliminates this risk.

Thanks to a simple block of code and loops you can do great things. For example, loops are used in bulk updates and in places such as lists.

While
The condition of the while loop is the same as the if statement. When the condition we write is true, it does the same thing as the if statement that runs the code block. The only difference is that the loop repeats as long as the condition is true.
Code:
<?php
    while(true) { // Condition
        // ...
    }


Example:
Code:
<?php
    $number = 0;
    
    while($number != 10) {
        echo "Number is not 10, number is {$number}<br>";
        
        $number++;
    }


Result:
Number is not 10, number is 0
Number is not 10, number is 1
Number is not 10, number is 2
Number is not 10, number is 3
Number is not 10, number is 4
Number is not 10, number is 5
Number is not 10, number is 6
Number is not 10, number is 7
Number is not 10, number is 8
Number is not 10, number is 9

Do While
This cycle is not used much like others, but it is worth knowing. The Do While loop is in fact the same difference as the Condition event of the While loop; The loop runs at least 1 time, whether or not the condition is valid. that is, once the code has run, it checks the condition in the 2nd rotation, that is all the humor.
Code:
<?php
    do {
        // ...
    }
    while (true); // Condition


Example:
Code:
<?php
    $number = 0;

    do {
       echo "Number is not 10, number is {$number}<br>";
        
        $number++;
    }
    while($number != 10);


Result:
Number is not 10, number is 0
Number is not 10, number is 1
Number is not 10, number is 2
Number is not 10, number is 3
Number is not 10, number is 4
Number is not 10, number is 5
Number is not 10, number is 6
Number is not 10, number is 7
Number is not 10, number is 8
Number is not 10, number is 9

For
The for loop is useful for using code that will return a certain number of times. 3 parameters are entered in the for loop. The first parameter defines the variable and its value to be used in the loop, the second parameter specifies the condition as in whil, and the third parameter specifies the action it will perform each time it returns.
Code:
<?php
    for(
        $i = 0; // Start
        $i == 10; // Condition
        $i++ // Action in each cycle
        ) {
            // ...
        }


Example:
Code:
<?php

    for($number = 0; $number != 10; $number++) {
        echo "Number is not 10, number is {$number}<br>";
    }


Result:
Number is not 10, number is 0
Number is not 10, number is 1
Number is not 10, number is 2
Number is not 10, number is 3
Number is not 10, number is 4
Number is not 10, number is 5
Number is not 10, number is 6
Number is not 10, number is 7
Number is not 10, number is 8
Number is not 10, number is 9

Foreach
The foreach loop can be called an array loop. Gets an array as the parameter, and returns the number of elements in the array. We can do a nice listing with just an array and foreach loop. It is simple to use but different from other cycles.

Loads the value of an element in each cycle in order to the variable we write after the as statement in the parameter.
Code:
<?php
    foreach(
        $array as $item // Each loop comes with an element in the array
    ) {
        // ...
    }


Example:
Code:
<?php

    $contacts = [
        [
            'username' => 'Lipaydi',
            'country'  => 'Turkey'
        ],
        [
            'username' => 'Asphyxia',
            'country'  => 'United States'
        ],
    ];

    
    foreach($contacts as $contact) {
        echo "{$contact['username']} is from {$contact['country']}.<br>";
    }


Result:
Lipaydi is from Turkey.
Asphyxia is from United States.

Thanks.
 
Top