#4 Loops in PHP

In this class of the part of control structures that makes up the php course of the web, I am going to treat the iterative control structures (loops).

After the conditional control structures, we could safely say that the loops or iterative control structures They are the most used resource in day-to-day programming. The loops, although basic, require a phase of practice and mastery to understand all their possibilities.

Content

  • Iterative programming structures
  • Features of loops in php
    • Initialization of php loop information
    • The condition of the loops in php
    • Updating php loops
  • Order of execution of iterative control structures
  • Extra: differences between loops with braces and without braces
  • Summary What are loops?
  • types of loops in php

Iterative programming structures

  • While a conditional allows us to indicate to the php interpreter under which condition it should execute one or more lines of code, loops extend this functionality and include the repetition of the code written between the braces.
  • We can then define iterative control structures, or loops, as the control structures that allow us to repeat as many times as we need the lines of code included between its braces. Loops are usually used to count or go through information grouped in variables of a special type for this purpose (vectors, arrays or matrices). In addition, thanks to the loops, repetitive problems or with an unknown that varies in time are solvable, problems that we would hardly be able to solve otherwise.
  • The basic functionality of a loop (which is what it does) marks the execution order of its content (the lines of code between braces) and its definition statement, that is, its header, where at least one condition to be checked usually appears, a condition that when cease to be fulfilled break the loopending with his execution.

Features of loops in php

The parts of a loop, or at least the points that we must be clear about when we use them, will generally be three:

  • The information initialization required for use in the loop. Example: a counter that starts at 0 and that we will increment later.
  • One condition. When it is true the lines of code inside it (between the braces) will be executed, when it is false the loop will end.
  • An update that controls the end of loop. If the condition was true when the loop started, at some point the information that controls the execution of the loop must be updated so that the loop terminates. If our loop doesn’t have an end condition it will be infinite and block our code interpreter until it can’t anymore (we’ve already messed it up!).

Let’s look at all three parts with a basic example, a loop while:

<?php
$i = 0;
while ( $i < 10 ) {
   $i = $i + 1;
}
?>

Initialization of php loop information

The initialization of variables or initialization refers to giving a value to the variables that we will need to use during the development of the execution of the loop iterations. For example, if we want a loop with ten iterations, we will need a variable that acts as a counter. This variable is sort of like counting on the fingers of lords loops. We must decide at what point this counter starts so that we can modify it later.

In the example I use the variable $i as counter, that is, in each iteration I will increase the value of the variable by one.

If you had not given a value to the variable before the loop statementthat is, if it had not initialized it, the condition of ($i < 10) could not have been fulfilled and the loop would never have been executed. Also, incrementing a variable without initializing it is a clear mistake.

Although the variables you use can be called whatever you want, the helper variable used as iteration counter of the loops is often called “$i”. Its name comes from the English word “iteration”.

The condition of the loops in php

Loops are similar to conditional control structures.: need a condition that marks when your code should run and when it should stop. Conditions can be multiple and can be strategically chained with AND (&&) or OR (||). We can accumulate as many conditions as we want and need. There are no restrictions in this regard, they only have to be evaluated finally as a boolean (TRUE either fake).

The example loop condition ($i < 10) It accomplishes three things: the loop starts, it performs a series of iterations, and it stops when the condition is no longer true.

As in the example seen, the condition of a loop together with the updating of the variables are the ones that determine how many repetitions will be carried out. If we increase the update, that is, if it were faster (for example, 2 by 2), the loop would finish faster.

Updating php loops

By updating an iterative structure, we refer to controlling the progress of the loop, that is, changing the value of the variables that mark the progress and end of the loop.

In the example, the update of the variable $i It is 1 in 1 ($i = $i + 1;)in total, 10 iterations will be carried out, from the value of $i 0 to 9Being the 10 the value that will evaluate to false and end the iterations of the loop.

Order of execution of iterative control structures

The execution order of a basic loop is:

  1. The loop condition is evaluated to see if it is true or false.
  2. If the condition is met (true) the lines of code between braces are executed.
  3. The interpreter reaches the last line of code.
  4. The interpreter returns to the loop definition statement and checks the condition again.
  5. If the condition is true once more, the code in the braces is executed again, otherwise the loop ends and your code in the braces is not executed.
  6. The execution order is repeated until the condition is no longer true.

A basic example to visualize the execution order of a loop, specifically a while loop, would be:

<?php
$a = 1;
while ( $a < 5 ) {
   echo 'El número es :' . $a . ' <br>;
   $a = $a + 1;
}
echo 'El número final es :' . $a;
?>

This is a basic usage representation of a WHILE loop. Like conditionals, it makes use of a condition in parentheses and a block of statements in braces on which it applies its effect.

This php WHILE loop performs an action as basic as counting from 1 to 5. The loop will repeat as long as the integer in variable $a is less than 5. When $a is equal to 5, since it will no longer be less than 5, the loop will end. The result displayed by executing the file will be:

the number is 1
the number is 2
the number is 3
the number is 4
the number is 5

The first four lines are controlled through the loop, if you look at the programming of the loop, the code that shows the first four lines I have only written 1 time, instead the result has been written 4 times. The last line shows the same line but with $a value 5. And it is that the line that increases $a has been executed 4 times, until $a that started with value 1 ends with 5.

Extra: differences between loops with braces and without braces

If the condition evaluates to true, the loop will execute its lines of code, that is, the ones that contain its keys or, if we don’t write them, the line of code will be executed just after its definition. So the following loop:

<?php
while ( $a < 5 ) {
  $a = $a + 2;
}
?>

It is equivalent to this one:

<?php
while ( $a < 5 ) 
  $a = $a + 2;
?>

The two previous examples have the same result. The omission of braces indicates that the loop only affects the next line to its definition. This use is only recommended in certain cases and when you have an advanced command of this control structure. In the examples that I will include I will always write the braces whether they are necessary or not.

Summary What are loops?

The concepts that define what loops are and how they work are:

  1. While a conditional allows us to indicate to the php interpreter under which condition it should execute one or more lines of code, loops extend this functionality and include the repetition of the code between braces.
  2. Iterative control structures, also known as loops, allow us to repeat the lines of code included between their braces as many times as we need.
  3. The functionality of a loop (which is what it does) marks the execution order of its content (the lines of code between braces) and its definition statement, that is, its header.
  4. Loops in PHP usually have three parts. clearly differentiated: initialization, condition and update.
  5. The initialization of variables or initialization refers to giving a value to the variables that we will need to use during the development of the execution of the loop iterations.
  6. They need a condition that marks when their code should run and when it should stop.
  7. By “updating an iterative structure” in PHP we mean monitoring the progress of the loop.
  8. If the condition is not met, the loop ends. The shell jumps to the line after the code that includes the loop (in braces), ie your code is not executed.

types of loops in php

In the following classes of the course we will see the most common and basic loops of php programming and programming in general, which will surely accompany you in your developments:

  • while loop
  • for loop
  • do-while loop

Leave a Reply