Related post

Inheritance in Java programming with example | Zhullyblog

PHP While, Do.. while, for loop - Zhullyblog




Hello, welcome to zhullyblog. If this is your first time here , please Subscribe to Zhullyblog by Email to get latest tutorial.


PHP Loops

What are loops?
Loops are used to execute block of code repeatedly until a certain condition is met. Loops are used to execute the same code over and over until a specified condition is met.

There are different types of loops but PHP supports four different types of loop.

• While loop
•Do…while loop
• For loop
• foreach loop

The while loop
The while loop is a control loop that runs until a certain condition is met.
The while loop will continue to run until the condition evaluate to true. The while loop always evaluate to true.

SYNTAX
   While(condition) {
        //The body of the code
    }

Let’s take a quick example
<?php

$x = 1;

while($x <= 10)
{
    echo "$x ";
    
}
?>



The do..while loop
The do while loop  evaluates the condition at the end of each loop.
This means that it runs the loop before checking the condition.


SYNTAX
   do {
    //Body of the code
 }
While(condition);

Lets take a quick example
<?php

$x = 1;

do {
    echo "$x";
 
} while($x <= 10)
?>


For loop
The for loop executes a statement as long as the condition stated is met. It is used to execute a statement for a specific number of times. This kind of loop is used when the user knows the number of times the loop will run.

SYNTAX
  For(initialization ; condition ; increment ) {
      //body of the code
}
Alright let’s take a good example

<?php

for($x = 1; $x <= 10; $x++)
{
    echo "$x <br/>";
}
?>


Please share









OTHER TUTORIALS
PHP Tutorial – Basic


1) Introduction to PHP



2) PHP Syntax


3) PHP Comment


4) PHP Data types


5) PHP Variables


6) PHP Echo and Print - How to display output


7) PHP If, if else and if elseif statement


9) PHP Strings : How to create and manipulate Strings


10) PHP Arrays










  • PHP Tutorial – Forms


  • 11)PHP FORMS: How to create a form, access data submitted in the form by user






    Other Tutorials










    Follow us on


    Subscribe to get latest tutorial and updates:



    Delivered by FeedBurner

    Comments