PHP Loops | while

Hi everyone,

Loops are code-blocks that are to be executed specified number of times, or while a specified condition is true.

We often want to execute some code blocks several times. To do this, in PHP we have four types of loops:

  • While loop
  • Do while loop
  • For loop
  • Foreach loop
In this class we are going to learn while loop.

While loop

The while loop executes a code block while a condition is true. The syntax is:

while(condition)
{
// code to be executed
}

The example below composes 5 lines of string and textboxes with while loop:

<?php
$i = 0;
while ($i < 5) {
    $i++;
    echo "Student " . $i . ")";
?>
<input type="text" name="student"/>
<?php
    echo "</br>";
}
?>
<input type="submit" name="send">
<?php
?>

The first line of while loop: $i++; makes the $i value 1 at first then it improves one by one.

The second line of while loop: it outputs Student and the value of $i variable(Student 1 for the first execution)

Then we stop coding php. We understand it from ?> tag

The fourth line of the loop: We put a text box named as student (this is a html code)

Then we begin to write php codes again. We understand it from <?php tag. Go to next line(echo “</br>”)

And end of loop. This loop will be executed 5 times because the $i variable compared with 5 in our while loop and its first value is 0. That means there will be five lines of strings and five textboxes in our browser.

The output will be:

In the scripts above, you saw some different types of usings <?php ?> tags and you see some html codes. Please don’t be afraid! As I said before we write php codes inside of html files, so anywhere you want to write html tag just close your php code block with ?> and go on with html. And also anywhere you want to add some php codes just add <?php tag and go on with your PHP codes. Yeah, that’s how it works. The differences between html and php codes are their syntax and php codes are executed in server so you cannot view your php codes in your browser by clicking view source. I want you to make sure about this. Lets check our source code from our browser:

<html>
   <head></head>
   <body>
      </br>Student 1)<input type="text" name="student"/>
      </br>Student 2)<input type="text" name="student"/>
      </br>Student 3)<input type="text" name="student"/>
      </br>Student 4)<input type="text" name="student"/>
      </br>Student 5)<input type="text" name="student"/>
      </br><input type="submit" name="send">
      </form>
   </body>
</html>

Instead of seeing php codes with html we can only see the html codes and the result(Each number in Student 1,2,etc. and textboxes are the results of our php code) that is returned by the server which executed our php codes.

Now you have learned how to do one task multiple times in a while loop. The other loops are similar to while loop with a little bit difference. I am going to talk about them later. Stay cool and stay connected! 😉

Leave a Reply

Your email address will not be published. Required fields are marked *