Loops in PHP

ad, authenticate, script, geeks corner, json, image, picture, server, file, write, read, close, fread, readfile, fclose, walden, systems, walden systems, php, functions, variable arguments, loops,  for loop, loop, while, do while, break, continue
PHP is a popular general-purpose scripting language that is especially suited to web development. Fast, flexible and pragmatic, PHP powers everything from your blog to the most popular websites in the world.

A loop is a basically a piece of code which runs several times until a specific condition is met. It is helpful as it reduces the lines of code within a script. The basic idea behind a loop is to automate the repetitive tasks within a program to save the time and effort. PHP supports four different types of loops.

while - loops through a block of code until the condition evaluates to true.

do…while — the code block is executed once and then condition is evaluated. If the condition is true the code block execution is repeated until the condition evaluates to true.

for — loops through a block of code until the counter reaches a specified number.

foreach — loops through a block of code for each element in an array.

While and Do .. While

The while statement executes a block of code until a specific condition is met. In the while loop, a condition is check and if true, the code block is executed. If the condition in a while loop fails from the beginning, the code block is never executed. To ensure that the code block gets executed at least once, we can use a do .. while loop which will execute the code block first and then check the condition. As with while loops in C, Python and other languages, if the condition never evaluates to false, the loop will go on infinitely.

$count = 1 ;
while ( $count < 5 )
{
    echo $count ;
    $count++ ; 
}


Will print out 1, 2, 3 , 4.
$control = 0 ;
$count = 1 ;
while ( $control < 5 )
{
    echo $count ;
    $count++ ; 
}


Will print out 1, 2, 3 , 4 . . . infinitely


For Loop

The for loop repeats a block of code until a certain condition is met. It is typically used to execute a block of code for certain number of times. This loop is preferred when we are sure about the number of iterations of the script. The parameters of for loop three meanings. Th first is initialization, used to initialize the counter variables and evaluated once unconditionally before the first execution of code block. Next is the condition which is the beginning of each iteration. If it evaluates to true, the loop continues and the nested statements are executed. If it evaluates to false, the execution of the loop ends. Lastly, he increment updates the loop counter with a new value. It is evaluate at the end of each iteration.

    for ($a = 0; $a <= 5; $a++) 
    {
        echo "$a, ";
    }


Will output 0, 1, 2, 3, 4, 5

Foreach Loop

It is used to execute a piece of code for each element in an array. Php foreach loop is specifically used for arrays and correspond to a each key in an array. The foreach loop statement only works with arrays and objects. If you use the foreach loop statement with other data types, you will get an error. In order to be able to directly modify array elements within the loop precede the element variable with a &. In this case the value will be assigned by reference.

    $num = [1,2,3];
    foreach ($num as $element) 
    {
         echo $element;
    }


Will output 123


    $num = [1,2,3];
    foreach ($num as &$element) 
    {
        $element *= 2;
    }
 
    print_r($num);


Will output [2,4,6]

Loops are very important in any programming whether it is C, Java, Python or any other language. Loops allows us to reuse blocks of code and gives us the ability to control the number of times blocks of code are executed. Like it if else statements in programming, it allows programmers to control what our program does given certain criteria.