Loops in PHP - ProgrammerTech
Language:

Search

Loops in PHP

  • Share this:
Loops in PHP

Loops

Loops in php, the concept of loop means loop in English and uses the loop. In general programming languages, we don't want to repeat code or anything we want multiple times. When we want to repeat a code several times, we just put it in the iteration loop. And we implement it and set conditions according to the progress of the program in which we work. We use rings in programming to make it easier for us not to write the same code multiple times.

If we want to use it multiple times, we put it inside a ring. And it will, in turn, repeat it according to our request. We will talk about loops using php, not much different from other languages, only references in php. It is possible to run a program execution with different control structures and in this lesson we will look at Loops. Loops can be used to repeat parts of a program a number of times or until a certain condition is met.

 

while loop

A while loop in php is used to repeat the execution of a specific sentence or group of sentences. This iteration stops when the number required in the condition is executed.

The general syntax for writing a while loop in php is:

while (condition) {
    statement
}

Before proceeding with anything, let's take a simple example to understand the working logic of the while loop function in php.

example 1

<?php
    while ($x <= 5){
        echo "<p>The text to be repeated</p>";
        $x = $x +1;
    }
?>

We talked at the beginning of this lesson that loops can be used to repeat certain parts of the script for a certain number of times or to fulfill a certain condition.

We use the while function and tell it that if the variable x is less than or equal to 5, it will print the statement in the print function. But so far the variable x does not have any data or data, nothing is saved inside the variable, so we said $ x = $ x +1;. In this case it will increase the value of x every time by 1 while repeating the above sentence. The sentence will be repeated 6 times, then the condition will be met, the loop will stop, and the output will be in the form on the server.

The text to be repeated
The text to be repeated
The text to be repeated
The text to be repeated
The text to be repeated
The text to be repeated

 

for loop

An iterative for loop is used to iterate the execution of a specified operation several times and takes the following form.

for (initialization condition step){
    statement
}

Example 2

<?php
    for($x=0; $x <= 50; $x = $x + 5){
        echo "<p> variable $x is now = " .  $x . "</p>";
    }
?>

We made loops using for and as we talked above that in loops a certain part or condition is repeated. And here we are telling the php interpreter there is a variable named x and this variable is saved from the data on the number 0 and this is the command 0 = x$. Then we tell the interpreter if x is less than or equal to 50 and this is the matter loops because we incremented the value of x every 5 times, not 1 as in the example before this. Then we print, note the output in the server.

variable 0 is now = 0
variable 5 is now = 5
variable 10 is now = 10
variable 15 is now = 15
variable 20 is now = 20
variable 25 is now = 25
variable 30 is now = 30
variable 35 is now = 35
variable 40 is now = 40
variable 45 is now = 45
variable 50 is now = 50

Notice how the value of x increases each time 5 .

example 3

<?php
    for ($x=1 ; $x <= 6 ; $x = $x + 1){
        echo "<h" . $x . "> heading level " . $x . "</h".$x.">";
    }
?>

Do you remember in the html in the headings lesson, there is a <h> tag that is responsible for heading. We know that the largest in the headers is <h1> and the smallest is <h6> and in the previous example we used that. where for will make a specific loops and a specific condition. In the previous example, we created a variable x that saves data on the number 1 and this is the code for that command 1 = x$. Then in each iteration, we write an address at the x level, i.e. h1, h2, then h3 ... etc., until the value of x becomes 6, and the output in the browser will be in the form.

The output of the for loop code in PHP

 

Loops Nested

As a principle there is no limit to how loops can be used, for example you can easily put loops inside loops and thus create many iterations. But beware PHP becomes slow and complex and the program gets bigger.

Example 4

<?php
    for ($KaissarRed = 0; $KaissarRed <= 255; $KaissarRed = $KaissarRed + 30){
       for ($KaissarGreen = 0 ; $KaissarGreen <=255; $KaissarGreen = $KaissarGreen + 30){
          for ($KaissarBlue = 0; $KaissarBlue <= 255; $KaissarBlue = $KaissarBlue + 30){
                $strcolor = "rgb(" . $KaissarRed . "," . $KaissarGreen . "," . $KaissarBlue . ")";
                echo "<span style='color:" . $strcolor . "'>" . $strcolor ."<br>"."</span>";
           }
     }
}
?> 

We created three loops where each loop contains a specific condition, all colors are red, green and blue. It can carry a value between 0 and 255 and this number that is specified in the condition is any combination of the three colors. Color rgb(255,255,255) and each of the selected variables will be incremented by 30.

The output in the browser will be like this.

Output of a loop code nested on the colors

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com